Imagemagick&枕头产生畸形的GIF框架 [英] Imagemagick & Pillow generate malformed GIF frames
问题描述
我需要提取gif动画的中间框架。
I need to extract the middle frame of a gif animation.
Imagemagick:
Imagemagick:
convert C:\temp\orig.gif -coalesce C:\temp\frame.jpg
正确生成框架:
但是当我提取单个框架时:
However when I extract a single frame:
convert C:\temp\orig.gif[4] -coalesce C:\temp\frame.jpg
然后框架格式错误,就好像-coalesce选项被忽略:
then the frame is malformed, as if the -coalesce option was ignored:
个人提取与枕头和ffmpeg的框架也导致格式错误的框架,测试了几个gifs。
Extraction of individual frames with Pillow and ffmpeg also results in malformed frames, tested on a couple of gifs.
下载gif: https://i.imgur.com/Aus8JpT.gif
我需要能够在PIL,ffmpeg(理想情况下是PIL)的Imagemagick中提取每个gif版本的中间帧。
I need to be able to extract middle frames of every gif version in either PIL, Imagemagick of ffmpeg (ideally PIL).
推荐答案
确定,此脚本将使用枕头找到并保存动画GIF的中间框架。
Ok, this script will find and save the middle frame of an animated GIF using Pillow.
它还将显示通过计算每个帧的毫秒,GIF的持续时间。
It will also display the duration of the GIF by counting the milliseconds of each frame.
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
这篇关于Imagemagick&枕头产生畸形的GIF框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!