Convert PIL Image to byte array?

后端 未结 3 1295
情书的邮戳
情书的邮戳 2020-12-04 16:44

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 ne

3条回答
  •  粉色の甜心
    2020-12-04 17:05

    Thanks everyone for your help.

    Finally got it resolved!!

    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()
    

    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.

提交回复
热议问题