Image.open() cannot identify image file – Python?

Image.open() cannot identify image file – Python?

I had a same issue.

from PIL import Image

instead of

import Image

fixed the issue

So after struggling with this issue for quite some time, this is what could help you:

from PIL import Image

instead of

import Image

Also, if your Image file is not loading and youre getting an error No file or directory then you should do this:

path=rC:ABCUsersPicturesimage.jpg

and then open the file

image=Image.open(path)

Image.open() cannot identify image file – Python?

In my case.. I already had from PIL import Image in my code.

The error occurred for me because the image file was still in use (locked) by a previous operation in my code. I had to add a small delay or attempt to open the file in append mode in a loop, until that did not fail. Once that did not fail, it meant the file was no longer in use and I could continue and let PIL open the file. Here are the functions I used to check if the file is in use and wait for it to be available.

def is_locked(filepath):
    locked = None
    file_object = None
    if os.path.exists(filepath):
        try:
            buffer_size = 8
            # Opening file in append mode and read the first 8 characters.
            file_object = open(filepath, a, buffer_size)
            if file_object:
                locked = False
        except IOError as message:
            locked = True
        finally:
            if file_object:
                file_object.close()
    return locked

def wait_for_file(filepath):
    wait_time = 1
    while is_locked(filepath):
        time.sleep(wait_time)

Leave a Reply

Your email address will not be published. Required fields are marked *