Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pil
#1
Greetings,
I can't get a Python PIL script to run, get "not an image" error.
Appreciate any pointers, to resolve this.

The code is:

for (root, dirs, files) in os.walk(img_path):
    for name in files:
        img = (os.path.join(root,name))
        print(img)
        try:
            #print(' "%s" ' % img)   ### "'%s'" % 
            #img = (img)                     << 1
            image = Image.open(img) ### images are color images
            print(image)                       << 2
            #MAX_SIZE = (224,224)
            #image.thumbnail(MAX_SIZE)
            #thumb = image.resize((224,224), Image.ANTIALIAS)
            #image.save(copy_path,name)
            #print()
        except:
            print("not an image")
The files (1) are:
C:\Users\u00\inspection_images\101MEDIA_11-18-21\100_20-0653.jpg
C:\Users\u00\inspection_images\101MEDIA_11-18-21\100_20-0654.jpg
C:\Users\u00\inspection_images\101MEDIA_11-18-21\100_20-0655.jpg

The output (2) are:
Output:
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=224x224 at 0x419DA90> <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=224x224 at 0x419D700> <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=224x224 at 0x419D0D0>
Yoriz write Mar-02-2022, 09:23 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

.py   os_walk2.py (Size: 3.31 KB / Downloads: 188)
Reply
#2
Please use correct tags when posting code.
This works for me

from os import walk
from PIL import Image

path = r'images'
copy_path = r'thumbs'
files = '*.png'
filename = []

for root, dir, files in walk(path):
    for img in files:
        try:
            image = Image.open(f'{path}/{img}')
            image.thumbnail((32,32))
            image.save(f'{copy_path}/thumb_{img}')
        except ValueError:
            print('Not a image')
BashBedlam likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
My guess is Python doesn't like "print(image) << 2", but it could also be a FileNotFoundError or an a "PIL.UnidentifiedImageError". With the try/except we'll never know. Unless you can really recover from an error and continue working, skip wrapping code in try/except. Let Python use the default exception handler that prints informative messages describing what error happened and where it happened in your code. This is a lot better than "Not a image"

At the very least do something like this:
for (root, dirs, files) in os.walk(img_path):
    for name in files:
        img = (os.path.join(root,name))
        print(img)
        try:
            image = Image.open(img) ### images are color images
            print(image) << 2
        except Exception as msg:
            print(msg)
Better yet, use the traceback library. This program fails in so many different ways. I got useful info for each failure and it kept running!
import sys
import traceback
from PIL import Image

for a in (42, "missing_file.txt", "test.txt", "dice1.png"):
    try:
        image = Image.open(a) ### images are color images
        print(image) << 2
    except Exception:
        print("\n\nError processing", a)
        traceback.print_exc(file=sys.stdout)
Error:
Error processing 42 Traceback (most recent call last): File ...", line 2957, in open fp.seek(0) AttributeError: 'int' object has no attribute 'seek' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "...", line 7, in <module> image = Image.open(a) ### images are color images File "...\lib\site-packages\PIL\Image.py", line 2959, in open fp = io.BytesIO(fp.read()) AttributeError: 'int' object has no attribute 'read' Error processing missing_file.txt Traceback (most recent call last): File "...", line 7, in <module> image = Image.open(a) ### images are color images File "...\lib\site-packages\PIL\Image.py", line 2953, in open fp = builtins.open(filename, "rb") FileNotFoundError: [Errno 2] No such file or directory: 'missing_file.txt' Error processing test.txt Traceback (most recent call last): File "...", line 7, in <module> image = Image.open(a) ### images are color images File "...\lib\site-packages\PIL\Image.py", line 3008, in open raise UnidentifiedImageError( PIL.UnidentifiedImageError: cannot identify image file 'test.txt' <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=97x97 at 0x198FA12E460> Error processing dice1.png Traceback (most recent call last): File "...", line 8, in <module> print(image) << 2 TypeError: unsupported operand type(s) for <<: 'NoneType' and 'int'
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020