Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open PIL image from byte file

Tags:

python

pillow

I have this image with size 128 x 128 pixels and RGBA stored as byte values 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 the exception

ValueError: not enough image data

Why? What am I doing wrong?

like image 819
Michael Dorner Avatar asked Oct 02 '15 13:10

Michael Dorner


People also ask

How do I view an image from PIL?

Python – Display Image using PIL To show or display an image in Python Pillow, you can use show() method on an image object. The show() method writes the image to a temporary file and then triggers the default program to display that image. Once the program execution is completed, the temporary file will be deleted.

How do I import photos from PIL?

To load the image, we simply import the image module from the pillow and call the Image. open(), passing the image filename. Instead of calling the Pillow module, we will call the PIL module as to make it backward compatible with an older module called Python Imaging Library (PIL).


1 Answers

The documentation for Image.open says that it can accept a file-like object, so you should be able to pass in a io.BytesIO object created from 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() 
like image 94
Colonel Thirty Two Avatar answered Oct 13 '22 04:10

Colonel Thirty Two