Python Forum

Full Version: Pil
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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>
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')
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'