首页 > 解决方案 > 使用 python Wand 制作 gif 时文件大小很大

问题描述

我正在尝试使用 Wand 进行液体重新缩放的 gif,但是当将 gif 保存到文件中时,文件大小非常,我的意思是,从 1539x2048 和 242 KB 的 JPG 图像会产生55 MB的 gif 。此外,调用image.make_blob()时生成 blob 需要很长时间。

from wand.image import Image

def gifynize(image: str, level: int = 50) -> bytes:

    with Image() as gif:
        with Image(filename=image) as source:
            width, height = source.width, source.height

            target_witdh = width * (100 - level) // 100
            target_height = height * (100 - level) // 100

            width_steps = (width - target_x) // (10 * 3)
            height_steps = (height - target_y) // (10 * 3)
           
            with source.clone() as clone:
                gif.sequence.append(clone)

            for _width, _height in zip(
                range(width, target_width, -width_steps), range(heigth, target_heigth, -heigth_steps)
            ):
                source.liquid_rescale(_width, height)
                with source.clone() as clone:
                    clone.resize(width, height) # Bring back to the original size
                    gif.sequence.append(clone)

        gif.format = "gif"
        return gif.make_blob()

我试过了gif.type = "optimize"gif.optimize_layers()gif.optimize_transparency()最终的 gif 文件大小没有改变。此外,当使用重图像 (2 MB) 时,它会在调用img.make_blob().

欢迎任何帮助。

标签: pythonimage-processinganimated-gifwandmagickwand

解决方案


推荐阅读