且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 PIL 图像转换为字节数组?

更新时间:2022-06-18 06:09:34

感谢大家的帮助.

终于解决了!!

import io

img = Image.open(fh, mode='r')
roi_img = img.crop(box)

img_byte_arr = io.BytesIO()
roi_img.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.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.