从字节文件

时间:2015-10-02 13:47:36

标签: python pillow

我的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:没有足够的图像数据

为什么呢?我做错了什么?

2 个答案:

答案 0 :(得分:87)

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

from PIL import Image
import io

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

答案 1 :(得分:10)

你可以试试这个:

avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x02
  源代码:
image = Image.frombytes('RGBA', (128,128), image_data, 'raw')
相关问题