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

查看:180
本文介绍了将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.

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

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