【发布时间】: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