import base64 import cv2 from PIL import Image from io import BytesIO def image(): img_file = 'demo.jpg' img = cv2.imread(img_file) image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) output_buffer = BytesIO() image.save(output_buffer, format="JPEG") byte_data = output_buffer.getvalue() base64_str = base64.b64encode(byte_data) print(str(base64_str, encoding = "utf-8"))