def image_to_byte_array(image: Image) -> bytes: # BytesIO is a fake file stored in memory imgByteArr = io.BytesIO() # image.save expects a file as a argument, passing a bytes io ins image.save(imgByteArr, format='png') # image.format # Turn the BytesIO object back into a bytes object imgByteArr = imgByteArr.getvalue() return imgByteArr