【发布时间】:2020-06-29 20:08:47
【问题描述】:
我正在编写一个循环遍历几个目录的脚本,找到该目录中的所有 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/stackoverflow/10.png
(可以将url中的10改成1-33之间的任意数字来获取每一个断帧)
这是完整的 gif:
https://ultimate-hitboxes.s3.amazonaws.com/stackoverflow/MarioBAir.gif
提前致谢!
【问题讨论】:
标签: python colors png python-imaging-library gif