from io import BytesIO import base64 def convert_image(img): buf = BytesIO() img.save(buf, format="PNG") byte_im = buf.getvalue() return byte_im