python - 从字节文件打开 PIL 图像

标签 python python-imaging-library

我有this image大小为 128 x 128 像素,RGBA 作为字节值存储在我的内存中。但是

from PIL import Image

image_data = ... # byte values of the image
image = Image.frombytes('RGBA', (128,128), image_data)
image.show()

抛出异常

ValueError: not enough image data

为什么?我做错了什么?

最佳答案

The documentation for Image.open说它可以接受类似文件的对象,因此您应该能够传入从包含编码图像的 bytes 对象创建的 io.BytesIO 对象:

from PIL import Image
import io

image_data = ... # byte values of the image
image = Image.open(io.BytesIO(image_data))
image.show()

关于python - 从字节文件打开 PIL 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46884404/

相关文章:

python - 寻找解决方案来检查图像大小是否符合我在 python 中的要求

python - Tensorflow 兼容模块问题?

python - 从文件夹中的图像序列获取 numpy 数组

python - 将圆形图转换为水平图

python - 在Python中向图像添加对齐文本

python-3.x - 如何以编程方式(最好在 python 中使用 PIL)计算具有剥离背景的对象的总像素数?

python - 在状态字典 : 0. 0.weight"、 "0.1.weight"、 "0.1.bias"、 "0.1.running_mean"中遇到缺少 key

python - 如何生成一天的时间戳?

python - 网页抓取Python

python - Pandas:当某些级别不匹配时,将一个多索引数据帧与另一个多索引数据帧进行切片