【问题标题】:Pyinstaller - OSError: cannot identify image file (PILLOW)Pyinstaller - OSError:无法识别图像文件(枕头)
【发布时间】:2019-05-01 18:17:06
【问题描述】:

我在 Python (3.7) 中创建了一个脚本,用于遍历目录并检查图像以计算主体占用了多少图像/计算图像中的空白量。

这在 Python 中有效,但是当使用 PyInstaller 转换为 windows exe 文件时,它会引发 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 是在行 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

【问题讨论】:

    标签: python python-imaging-library pyinstaller


    【解决方案1】:

    我最近也遇到了同样的情况。如果我们的情况相同,你可以使用其他图像格式文件(.png、.bmp 等)但 .jpg 扩展名文件对吗?

    问题:

    当您在 pipenv 等虚拟环境中使用 PyInstaller 编译项目时发生这种情况。

    解决方案:

    你应该使用Pip来安装你程序的所有依赖包,包括PyInstaller,并在真实环境中直接使用PyInstaller将你的程序转换成windows exe文件。

    原因:

    我不是专家,所以我真的不知道。我猜 PyInstaller 在虚拟环境中使用时无法使用某些 windows dll。

    【讨论】:

    • 完美!谢谢,现在我想起来了
    • 有没有什么办法可以让它在 virtualenv 中工作
    猜你喜欢
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多