Python 将图像分割成帧,颜色混乱

Python split image into frames, messes with colors

我正在编写一个循环遍历几个目录的脚本,找到该目录中的所有 gif,将它们拆分为单独的 png 帧图像,然后将它们写入目录。

实际拆分工作得很好,但是除了第一帧之外的所有帧都搞砸了。根据我的研究,我了解一些 gif,而不是存储所有单独的帧,只存储帧之间的变化,我猜这是基于我得到的输出的情况之一。

这是我使用的代码:

from PIL import Image
from os import listdir
import os
from os.path import isfile, join

characterDirs = ["01_mario"]
print(characterDirs)

#Loop through characterDirs
for char in characterDirs:

    #save each gif into a directory for easy access
    moves = [f for f in listdir("./media/gifs/" + char)]

    #Make a directory for that character
    os.mkdir("./media/frames/" + char)

    #Loop through all moves in the array
    for move in moves:
        print(move)
        i=0

        #Open the gif
        gif = Image.open("./media/gifs/" + char + "/" + move)

        #Make a directory for the move
        os.mkdir("./media/frames/" + char + "/" + move[0:-4])

        #Keep going until there are no remaining frames of the gif
        while True:
            try:

                #Save the frame
                gif.save("./media/frames/" + char + "/" + move[0:-4] + "/" + str(i+1) + ".png")

                #Increment to next frame
                gif.seek(gif.tell()+1)
                i +=1
            except EOFError:
                break

这是我得到的几帧:

https://ultimate-hitboxes.s3.amazonaws.com/Whosebug/10.png

(您可以将 url 中的 10 更改为 1-33 之间的任意数字以获得每个破帧)

这是完整的 gif:

https://ultimate-hitboxes.s3.amazonaws.com/Whosebug/MarioBAir.gif

提前致谢!

尝试在终端中使用 ImageMagick,如下所示:

convert MarioBAir.gif -coalesce frame-%02d.png

这将为您提供 33 个独立的框架:

文件名是:

-rw-r--r--     1 root  staff     30873 18 Mar 17:45 frame-00.png
-rw-r--r--     1 root  staff     31971 18 Mar 17:45 frame-01.png
-rw-r--r--     1 root  staff     75743 18 Mar 17:45 frame-02.png
-rw-r--r--     1 root  staff     73075 18 Mar 17:45 frame-03.png
-rw-r--r--     1 root  staff     34927 18 Mar 17:45 frame-04.png
-rw-r--r--     1 root  staff     35757 18 Mar 17:45 frame-05.png
...
...
-rw-r--r--     1 root  staff     72723 18 Mar 17:45 frame-31.png
-rw-r--r--     1 root  staff     72103 18 Mar 17:45 frame-32.png

如果使用 v7 ImageMagick,命令变为:

magick MarioBAir.gif -coalesce frame-%02d.png