【问题标题】:Gif to base64 encoding without saving fileGif 到 base64 编码而不保存文件
【发布时间】:2020-05-25 15:46:16
【问题描述】:

我的 gif 只是保存到 .gif 文件的一组图像。但是我需要对gif文件进行编码,有没有办法不将文件保存为.gif并对其进行编码?

imageio.mimsave(output_vid + '.gif', [img_as_ubyte(frame) for frame in gif], fps=fps)

【问题讨论】:

    标签: python animated-gif tobase64string


    【解决方案1】:

    如果您希望在内存中对图像进行 base64 编码,您可以使用以下函数:

    import base64
    import io
    
    def _img_to_base64(image, max_size):
        if type(image) is np.ndarray:
            image = Image.fromarray(image)
        elif type(image) is str or type(image) is str_:
            image = Image.open(image)
        output = io.BytesIO()
        image = resize_with_aspect_ratio(image, max_size)
        image.save(output, format='PNG')
        b64 = str(base64.b64encode(output.getvalue()).decode('utf-8'))
        return b64
    

    如果您愿意分享更多关于您的输入和期望输出的详细信息,我可能会提供更多帮助

    【讨论】:

      猜你喜欢
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      相关资源
      最近更新 更多