简体   繁体   English

为什么在尝试从 io.bytesIO object 打开图像时引发 PIL.UnidentifiedImageError?

[英]Why is PIL.UnidentifiedImageError being raised when trying to open an image from an io.bytesIO object?

In short terms, when trying to open an image using byte data, I unfortunately end up with an error.简而言之,当尝试使用字节数据打开图像时,不幸的是我以错误告终。 Here is some of the code for more clarity.为了更清楚起见,这里有一些代码。

test2.py:测试2.py:

logo = b"iVBORw0KGgoAAAA ... "
#(It's 60k characters long don't worry about it)

test.py:测试.py:

import test2 as pim
import io
from PIL import Image, ImageTk

sol = io.BytesIO(pim.logo)
image = Image.open(sol)

Apparenly for some strange reason, I end up with this error:显然出于某种奇怪的原因,我最终遇到了这个错误:

PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x0000028D819373D0>

I've tried searching on other posts.我试过搜索其他帖子。 I found some similar ones, but none of the solutions worked.我找到了一些类似的,但没有一个解决方案有效。

@martineau actually answered the question but here is a quick runup: @martineau 实际上回答了这个问题,但这里有一个快速启动:

Convert base64 data, use it in a bytesio class and put it into an image:转换 base64 数据,在 bytesio class 中使用并放入图像中:

s = base64.b64decode(pim.logo)
sol = io.BytesIO(s)
image = Image.open(sol)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PIL.UnidentifiedImageError:无法识别图像文件 <_io.BytesIO object - PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object 引发 UnidentifiedImageError(PIL.UnidentifiedImageError: 无法识别图像文件 &lt;_io.BytesIO object at 0x0000018CA596D350&gt; - raise UnidentifiedImageError( PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x0000018CA596D350> Python 错误:PIL.UnidentifiedImageError:无法识别图像文件 &lt;_io.BytesIO object at 0x1144e9860&gt; - Python error: PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x1144e9860> PIL 无法识别 io.BytesIO object 的图像文件 - PIL cannot identify image file for io.BytesIO object UnidentifiedImageError: 无法识别图像文件 &lt;_io.BytesIO object at 0x000002154C6AE400&gt; - UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000002154C6AE400> 打开 fp = io.BytesIO(fp.read()) AttributeError: &#39;str&#39; 对象在 PIL/image.py 中没有属性 &#39;read&#39; - open fp = io.BytesIO(fp.read()) AttributeError: 'str' object has no attribute 'read' in PIL/image.py OSError:在尝试使用 Keras 模型进行预测时,无法识别图像文件 &lt;_io.BytesIO object at ... &gt; - OSError: cannot identify image file <_io.BytesIO object at ... > when trying to predict with Keras model OSError:尝试从s3获取图像并打开图像时,无法识别图像文件&lt;_io.BytesIO对象位于0x00000198001B9E08&gt; - OSError: cannot identify image file <_io.BytesIO object at 0x00000198001B9E08> while trying to get images from s3 and open image python PIL _io.BytesIO无法读取用画布转换的图像 - A image converted with canvas fails to be read by python PIL _io.BytesIO 从 io.BytesIO 保存图像 - Save image from io.BytesIO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM