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"))
本文介绍了一个使用Python进行图像处理的例子,包括读取图片文件,转换颜色空间,将图片转换为JPEG格式并保存到内存缓冲区,最后将图片数据编码为Base64字符串。涉及的技术包括OpenCV用于读取和显示图像,PIL用于图像操作,以及base64模块用于数据编码。

1540

被折叠的 条评论
为什么被折叠?



