将PIL图像转换为字节数组?
问题描述:
我有PIL图像格式的图像。我需要将其转换为字节数组。
I have an image in PIL Image format. I need to convert it to byte array.
img = Image.open(fh, mode='r')
roiImg = img.crop(box)
现在我需要 roiImg
作为字节数组。
Now I need the roiImg
as a byte array.
答
感谢大家的帮助。
终于解决了!!
import io
img = Image.open(fh, mode='r')
roiImg = img.crop(box)
imgByteArr = io.BytesIO()
roiImg.save(imgByteArr, format='PNG')
imgByteArr = imgByteArr.getvalue()
这样我就不必将裁剪后的图像保存在硬盘中了我能够从PIL裁剪图像中检索字节数组。
With this i don't have to save the cropped image in my hard disc and I'm able to retrieve the byte array from a PIL cropped image.