![](/img/trans.png)
[英]pillow OSError: cannot identify image file <_io.BytesIO object at 0x02345ED8>
[英]Pyinstaller - OSError: cannot identify image file (PILLOW)
我在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
我最近遇到了同樣的情況。 如果我們的情況相同,你可以使用其他圖像格式文件(.png,.bmp等),但.jpg擴展文件對嗎?
它發生在您使用PyInstaller在pipenv等虛擬環境中編譯項目時。
您應該使用Pip來安裝程序的所有包依賴項,包括PyInstaller並直接在真實環境中使用PyInstaller將程序轉換為windows exe文件。
我不是專家,所以我真的不知道。 我猜PyInstaller在虛擬環境中使用它時無法使用某些windows dll。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.