我正在用Python编写一个程序,批量将动画WebP文件转换为GIF,但我遇到了一个问题,我无法确定原始文件的帧速率,因为我的PIL不会返回每帧的持续时间。
目前我正在使用PIL库来打开和分析WEBP文件。根据其他在线帖子和 PIL 文档,我应该能够使用 .info 查看每帧的持续时间,但目前我得到的只是“循环”和“背景”属性。
我正在使用此处链接的 webp 文件来测试代码: https://mathiasbynens.be/demo/animated-webp 使用其他 webp 文件也有同样的问题,所以我很确定它不是这个文件独有的。
from PIL import Image, ImageSequence
def webp_gif(filename, outfile):
imagefile = Image.open(filename)
#checking frame by frame fails too
for frame in ImageSequence.Iterator(imagefile):
frameinfo = frame.info
#This throws an error
duration = 1000 / imagefile.info['duration']
images = processImage("%s" % filename)
fps = duration / len(images)
我不知道为什么你无法从 PIL 获得延迟,所以这实际上是一个解决方法的建议,但对于评论来说太长了。
您曾经能够获得像this这样的延迟。
您可以使用 ImageMagick 获取图像中所有帧的延迟/持续时间和处理方法,如下所示:
magick animated-webp-supported.webp -format "Frame: %s, Dispose: %D, Delay (centiseconds): %T\n" info:
Frame: 0, Dispose: None, Delay (centiseconds): 7
Frame: 1, Dispose: None, Delay (centiseconds): 7
Frame: 2, Dispose: None, Delay (centiseconds): 7
Frame: 3, Dispose: None, Delay (centiseconds): 7
Frame: 4, Dispose: None, Delay (centiseconds): 7
Frame: 5, Dispose: None, Delay (centiseconds): 7
Frame: 6, Dispose: None, Delay (centiseconds): 7
Frame: 7, Dispose: None, Delay (centiseconds): 7
Frame: 8, Dispose: None, Delay (centiseconds): 7
Frame: 9, Dispose: None, Delay (centiseconds): 7
Frame: 10, Dispose: None, Delay (centiseconds): 7
Frame: 11, Dispose: None, Delay (centiseconds): 7
如果您想使用 Python,您可以使用
wand
https://pypi.org/project/Wand/ 进行与上面完全相同的操作,这是 ImageMagick 的 Python ctypes 绑定。
您还可以使用
exiftool
获取延迟,如下所示:
exiftool -duration animated-webp-supported.webp
Duration : 0.84 s
如果您想使用 Python 来做到这一点,请参阅 this 答案。我还没有调查为什么这两个工具报告不同的值。