import io from PIL import Image def image_to_bytes(image: Image.Image): bio = io.BytesIO() image.save(bio, format="png") return bio.getvalue()