Imagemagick 和 Pillow 生成格式错误的 GIF 帧
Imagemagick & Pillow generate malformed GIF frames
我需要提取gif动画的中间帧
Imagemagick:
convert C:\temp\orig.gif -coalesce C:\temp\frame.jpg
正确生成帧:
但是当我提取单帧时:
convert C:\temp\orig.gif[4] -coalesce C:\temp\frame.jpg
然后框架格式不正确,好像忽略了 -coalesce 选项:
使用 Pillow 和 ffmpeg 提取单个帧也会导致格式错误的帧,已在几个 gif 上进行测试。
下载 gif:https://i.imgur.com/Aus8JpT.gif
我需要能够在 PIL、ffmpeg 的 Imagemagick(最好是 PIL)中提取每个 gif 版本的中间帧。
你可以这样做:
convert pour.gif -coalesce -delete 0-3,5-8 frame4.png
基本上,它生成完整的所有帧,然后删除除 4 以外的所有帧。
好的,此脚本将使用 Pillow 查找并保存动画 GIF 的中间帧。
它还会通过计算每一帧的毫秒数来显示 GIF 的持续时间。
from PIL import Image
def iter_frames(im):
try:
i = 0
while 1:
im.seek(i)
frame = im.copy()
if i == 0:
# Save pallete of the first frame
palette = frame.getpalette()
else:
# Copy the pallete to the subsequent frames
frame.putpalette(palette)
yield frame
i += 1
except EOFError: # End of gif
pass
im = Image.open('animated.gif')
middle_frame_pos = int(im.n_frames / 2)
durations = []
for i, frame in enumerate(iter_frames(im)):
if i == middle_frame_pos:
middle_frame = frame.copy()
try:
durations.append(frame.info['duration'])
except KeyError:
pass
middle_frame.save('middle_frame.png', **frame.info)
duration = float("{:.2f}".format(sum(durations)))
print('Total duration: %d ms' % (duration))
有用代码:
- Python: Converting GIF frames to PNG
- https://github.com/alimony/gifduration
您正在尝试将单个输入图像合并为单个输出图像。所求即所得
相反,您应该 "flatten" 将帧 0-4 转换为单个输出图像:
convert C:\temp\orig.gif[0-4] -flatten C:\temp\frame.jpg
如果您使用“-coalesce”,您将在 frame-0.jpg 到 frame-4.jpg 中获得 5 帧输出,最后一个是您想要的图像。
我需要提取gif动画的中间帧
Imagemagick:
convert C:\temp\orig.gif -coalesce C:\temp\frame.jpg
正确生成帧:
但是当我提取单帧时:
convert C:\temp\orig.gif[4] -coalesce C:\temp\frame.jpg
然后框架格式不正确,好像忽略了 -coalesce 选项:
使用 Pillow 和 ffmpeg 提取单个帧也会导致格式错误的帧,已在几个 gif 上进行测试。
下载 gif:https://i.imgur.com/Aus8JpT.gif
我需要能够在 PIL、ffmpeg 的 Imagemagick(最好是 PIL)中提取每个 gif 版本的中间帧。
你可以这样做:
convert pour.gif -coalesce -delete 0-3,5-8 frame4.png
基本上,它生成完整的所有帧,然后删除除 4 以外的所有帧。
好的,此脚本将使用 Pillow 查找并保存动画 GIF 的中间帧。
它还会通过计算每一帧的毫秒数来显示 GIF 的持续时间。
from PIL import Image
def iter_frames(im):
try:
i = 0
while 1:
im.seek(i)
frame = im.copy()
if i == 0:
# Save pallete of the first frame
palette = frame.getpalette()
else:
# Copy the pallete to the subsequent frames
frame.putpalette(palette)
yield frame
i += 1
except EOFError: # End of gif
pass
im = Image.open('animated.gif')
middle_frame_pos = int(im.n_frames / 2)
durations = []
for i, frame in enumerate(iter_frames(im)):
if i == middle_frame_pos:
middle_frame = frame.copy()
try:
durations.append(frame.info['duration'])
except KeyError:
pass
middle_frame.save('middle_frame.png', **frame.info)
duration = float("{:.2f}".format(sum(durations)))
print('Total duration: %d ms' % (duration))
有用代码:
- Python: Converting GIF frames to PNG
- https://github.com/alimony/gifduration
您正在尝试将单个输入图像合并为单个输出图像。所求即所得
相反,您应该 "flatten" 将帧 0-4 转换为单个输出图像:
convert C:\temp\orig.gif[0-4] -flatten C:\temp\frame.jpg
如果您使用“-coalesce”,您将在 frame-0.jpg 到 frame-4.jpg 中获得 5 帧输出,最后一个是您想要的图像。