Open PIL image from byte file

I have this image of 128 x 128 pixels and RGBA saved in bytes in my memory. But

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

throws an exception

ValueError: insufficient image data

Why? What am I doing wrong?

+8
python pillow
source share
1 answer

The documentation for Image.open says that it can accept a file-like object, so you should be able to pass in the io.BytesIO object created with the bytes object containing the encoded image:

 from PIL import Image import io image_data = ... # byte values of the image image = Image.open(io.BytesIO(image_data)) image.show() 
+28
source share

All Articles