小编roc*_*r31的帖子

修改 GIF 中的所有帧 - 拆分为帧、处理每个帧并创建新的 GIF

我一直在尝试 PIL/Pillow,但遇到了困难。我一直在尝试拍摄 GIF,将其分割成帧,修改每个帧的颜色深度,然后再次将帧连接成 GIF。

这是我的代码:

from PIL import Image

def gif_depth_change(pathToGIF, colourDepth):
    originalGIF = Image.open(pathToGIF)
    newGIF = originalGIF.convert("P", palette=Image.ADAPTIVE, colors=colourDepth)
    newGIF.show()
Run Code Online (Sandbox Code Playgroud)

convert()方法在这里似乎不起作用,因为它只显示一个没有颜色深度作为参数的 PNG。

我也尝试过这个:

def gif_depth_change(pathToGIF, colourDepth):
    originalGIF = Image.open(pathToGIF)
    newFrames = []
    for frame in range(0, originalGIF.n_frames):
        originalGIF.seek(frame)
        x = originalGIF.convert("P", palette=Image.ADAPTIVE, colors=colourDepth)
        newFrames.append(x)
    newFrames[0].save('changed-depth-gif.gif', format='GIF', append_images=newFrames[1:], save_all=True)
Run Code Online (Sandbox Code Playgroud)

运行时,此代码会保存一个 GIF,但不会以任何方式修改它(它给了我相同的 GIF)。我也尝试使用convert()onoriginalGIF.seek(frame)但返回了None

python gif python-imaging-library

5
推荐指数
1
解决办法
1150
查看次数

标签 统计

gif ×1

python ×1

python-imaging-library ×1