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?
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.
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).
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With