简体   繁体   中英

Pyinstaller - OSError: cannot identify image file (PILLOW)

I've created a script in Python (3.7) to go through a directory and check the images to calculate how much of the image is taken up by the subject/calculate the amount of white space in the image.

This works in Python however when using PyInstaller to convert to a windows exe file it throws a OSError

for filename in os.listdir(path):
        image = Image.open(filename)
        width, height = image.size

        # Check if each pixel in image is white (255, 255, 255) and calculate percentage of image is white
        bg_count = next(n for n, c in image.getcolors(width * height) if c == (255, 255, 255))
        img_count = width * height - bg_count
        img_percent = img_count * 100.0 / width / height
        image.close()

        # If image doesn't meet requirements add to a csv created before the for loop
        if img_percent >= percentage:
            output_file.write(f"{filename} , {img_percent}%")
            output_file.write("\n")
            output_count += 1

OSError is caused on line image = Image.open(filename)

Traceback (most recent call last):
   File "main.py", line 47, in <module>
   File "main.py", line 23, in main
   File "site-packages\PIL\Image.py", line 2705, in open
OSError: cannot identify image file '1640681.jpg'
[5132] Failed to execute script main

I've faced the same situation recently. If our circumstance are the same, you may work well with other image format file (.png, .bmp, etc.) but .jpg extension file right?

Problem:

It happened when you compiled your project using PyInstaller in the virtual environment like pipenv.

Solution:

You should use Pip to install all of the package dependencies of your program includes PyInstaller and use PyInstaller directly in the real environment to convert your program into windows exe file.

Reason:

I'm not expert, so i don't really know. I guess PyInstaller couldn't use some windows dll when you use it inside virtual environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM