[英]Open PIL image from byte file
我有這個圖像大小為 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:沒有足夠的圖像數據
為什么? 我究竟做錯了什么?
Image.open
的文檔說它可以接受類似文件的對象,因此您應該能夠傳入從包含編碼圖像的bytes
對象創建的io.BytesIO
對象:
from PIL import Image
import io
image_data = ... # byte values of the image
image = Image.open(io.BytesIO(image_data))
image.show()
你可以試試這個:
image = Image.frombytes('RGBA', (128,128), image_data, 'raw')
源代碼: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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.