![](/img/trans.png)
[英]Why is PIL.UnidentifiedImageError being raised when trying to open an image from an io.bytesIO object?
[英]PIL open() method not working with BytesIO
出於某種原因,當我嘗試從BytesIO蒸汽制作圖像時,它無法識別圖像。 這是我的代碼:
from PIL import Image, ImageGrab
from io import BytesIO
i = ImageGrab.grab()
i.resize((1280, 720))
output = BytesIO()
i.save(output, format = "JPEG")
output.flush()
print(isinstance(Image.open(output), Image.Image))
它拋出的錯誤的堆棧跟蹤:
Traceback (most recent call last):
File "C:/Users/Natecat/PycharmProjects/Python/test.py", line 9, in <module>
print(isinstance(Image.open(output), Image.Image))
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2126, in open
% (filename if filename else fp))
IOError: cannot identify image file <_io.BytesIO object at 0x02394DB0>
我正在使用PIL的Pillow實現。
將BytesIO視為文件對象,在完成圖像寫入后,文件的光標位於文件的末尾,因此當Image.open()
嘗試調用output.read()
,它會立即獲得EOF。
在將output
傳遞給Image.open()
之前,需要添加output.seek(0)
Image.open()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.