目的:
将图像数据数组转换为 Base64 编码的字符串,这样的转换通常在将图像数据嵌入到网页或在网络上传输图像数据时使用。
import base64
from PIL import Image
from io import BytesIO
#这个函数的主要目的是将图像数据数组转换为 Base64 编码的字符串,这样的转换通常在将图像数据嵌入到网页或在网络上传输图像数据时使用。
def image2base64(images_arrays):
# 将rgb通道顺序换下
image_array = images_arrays[:, :, [2, 1, 0]]
# 将三维数组转换为图像
# 将图像转换为Base64编码字符串
image_pil = Image.fromarray(image_array)
buffer = BytesIO()
image_pil.save(buffer, format="PNG")
base64_image = base64.b64encode(buffer.getvalue()).decode('utf-8')
return base64_image