import io from PIL import Image # Convert image to bytes def image_to_bytes(image: Image): img_byte_arr = io.BytesIO() image.save(img_byte_arr, format='PNG') return img_byte_arr.getvalue() def bytes_to_image(image_bytes: bytes): return Image.open(io.BytesIO(image_bytes))