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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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()
Method 2
You can try this:
image = Image.frombytes('RGBA', (128,128), image_data, 'raw')
Source Code:
def frombytes(mode, size, data, decoder_name="raw", *args): param mode: The image mode. param size: The image size. param data: A byte buffer containing raw data for the given mode. param decoder_name: What decoder to use.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0