将 PIL 图像转换为字节数组? [英] Convert PIL Image to byte array?

查看:38
本文介绍了将 PIL 图像转换为字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PIL Image 格式的图像.我需要将其转换为字节数组.

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')
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.

这篇关于将 PIL 图像转换为字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆