python中将base64数据写成图片

49次阅读
没有评论

共计 558 个字符,预计需要花费 2 分钟才能阅读完成。

在 Python 中,可以使用 base64 库将 base64 数据转换为图片。以下是将 base64 数据写成图片的示例代码:

import base64
import io
from PIL import Image

def write_base64_image(base64_data, file_path):
    # 解码 base64 数据
    image_data = base64.b64decode(base64_data)
    
    # 创建 Image 对象
    image = Image.open(io.BytesIO(image_data))
    
    # 保存图片
    image.save(file_path)

# 示例调用
base64_data = "base64 数据 "
file_path = " 图片保存路径 "
write_base64_image(base64_data, file_path)

在示例代码中,write_base64_image函数接受两个参数,分别是 base64 数据和要保存的文件路径。首先,使用 base64.b64decode 函数将 base64 数据解码为原始的图片数据。然后,使用 io.BytesIO 将图片数据转换为 BytesIO 对象,并传递给 Image.open 函数创建 Image 对象。最后,使用 Image.save 方法将图片保存为指定的文件路径。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2024-02-05发表,共计558字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)