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.
import io
from PIL import Image
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 on my disk and I am able to retrieve the byte array from a PIL cropped image.