用python分割并提取gif动画帧
Sep
10
用python分割并提取gif动画帧
由于工作中要使用到裁图,可是发现一个很郁闷的事情就是当对帧数很多的gif图片进行裁图时会超级慢,导致系统的负载狂升,不得已得寻找办法。
能不能只提取其中的一部分动画帧,这样就可以避免帧数过多导致down机,虽然这样会导致GIF动画显示不全,或者不连贯,可是也是不得已而为之了。
找了N多资料都没有提到如何提取gif帧,连gif的w3c官方文档上都没有提到帧,真是郁闷呢。
找啊,找啊,功夫不负啊,在使用ultraedit对gif图片进行16进制分析时,终于让偶找到了,gif居然是每帧之间使用”, “进行分割的,相应的16进制代码是2C 00 00 00
而结束则是使用”;”,相应的16进制是3B
这样就好办了,以下是python的一段代码
注意: 如果有需求的话,可以加上是否是符合gif89a或者gif87a的格式进行判断,因为有些图片看起来是jpg后缀,其实是gif格式的:
#!/bin/python
giffile=open('c:/22.gif','rb')
'''each frame split by ', ' this is use binary system'''
f_sp=','+chr(00)+chr(00)+chr(00)+chr(00)
'''gif close by ';' this is use binary system'''
f_cl=chr(59)
eachf=giffile.read().split(f_sp)
result=eachf[:10]
result.append(eachf[-1])
newfile=f_sp.join(result)
giffile.close()
newgif=open(’c:/newgif2.gif’,'wb’)
newgif.write(newfile)
newgif.close()
—————————————————————————
2008.10.15
找到了一种更方便的方法,使用ImageMagick中带的convert命令可以直接进行转换:
convert “source.gif[0-19]” target.gif
这样就直接截取了前20帧
^_^


















Comment by nova 2 hd apk | [Reply]
用python分割并æå–gif动画帧 I was suggested this website by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my difficulty. You’re wonderful! Thanks! your article about 用python分割并æå–gif动画帧 Best Regards Cassetta Cassetta
Comment by invest liberty reserve | [Reply]
An absorbing discussion is couturier remark. I cerebrate that you should create author on this message, it power not be a preconception someone but mostly fill are not sufficiency to mouth on much topics. To the next. Cheers like your 用python分割并æå–gif动画帧.