【问题标题】:How to convert a base64 image to an image and save it to the file system [duplicate]如何将base64图像转换为图像并将其保存到文件系统[重复]
【发布时间】:2021-10-29 04:20:57
【问题描述】:

我有一个字符串:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjwAAAJbCAIAAABvnNkvAAAACXBIWXM и т.д.

已满:https://pastebin.com/tfDThBnE

如何将图片转换成png和jpg并保存到文件系统中?

【问题讨论】:

    标签: python image save converters


    【解决方案1】:
    from PIL import Image
    import base64
    from io import BytesIO
    
    data = 'copy_your_raw_data_here'
    # you need to remove the prefix 'data:image/png;base64,'
    
    bytes_decoded = base64.b64decode(data)
    
    
    img = Image.open(BytesIO(bytes_decoded))
    img.show()
    
    # to jpg
    out_jpg = img.convert("RGB")
    
    # save file
    out_jpg.save("saved_img.jpg")
    

    如果您没有 PIL(Pillow) 库,请确保先安装它。

    pip install pillow
    

    它是一个python imgae 处理程序包。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-20
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多