Fix Python – Convert PIL Image to byte array?


Asked By – Evelyn Jeba

I have an image in PIL Image format. I need to convert it to byte array.

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

Now I need the roiImg as a byte array.

Now we will see solution for issue: Convert PIL Image to byte array?


Thanks everyone for your help.

Finally got it resolved!!

import io

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

img_byte_arr = io.BytesIO(), 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.

This question is answered By – Evelyn Jeba

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0