Convert PIL Image to byte array?

2019-02-01 20:37发布

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)

Now I need the roiImg as a byte array.

1条回答
Melony?
2楼-- · 2019-02-01 20:54

Thanks everyone for your help.

Finally got it resolved!!

import io

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

imgByteArr = io.BytesIO()
roiImg.save(imgByteArr, format='PNG')
imgByteArr = imgByteArr.getvalue()

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.

查看更多
登录 后发表回答