目录
1 导入依赖
import base64
from PIL import Image
import io
2 image_to_base64
def image_to_base64(image_path):
"""
将图片文件转换成Base64编码的字符串
:param image_path: 图片文件的路径
:return: Base64编码的字符串
"""
with open(image_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
return encoded_string
3 base64_to_image
def base64_to_image(base64_string):
img_data = base64.b64decode(base64_string)
img_io = io.BytesIO(img_data)
img = Image.open(img_io)
return img
至此,本文分享的内容结束啦。
标签:string,img,PYTHON,image,base64,io,互转,import From: https://blog.csdn.net/wss794/article/details/142948567