Python图像处理之gif动态图的解析与合成操作详解-创新互联
本文实例讲述了Python图像处理之gif动态图的解析与合成操作。分享给大家供大家参考,具体如下:
成都创新互联-专业网站定制、快速模板网站建设、高性价比盐山网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式盐山网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖盐山地区。费用合理售后完善,10余年实体公司更值得信赖。gif动态图是在现在已经司空见惯,朋友圈里也经常是一言不合就斗图。这里,就介绍下如何使用python来解析和生成gif图像。
一、gif动态图的合成
如下图,是一个gif动态图。
gif动态图的解析可以使用PIL
图像模块即可,具体代码如下:
#-*- coding: UTF-8 -*- import os from PIL import Image def analyseImage(path): ''' Pre-process pass over the image to determine the mode (full or additive). Necessary as assessing single frames isn't reliable. Need to know the mode before processing all frames. ''' im = Image.open(path) results = { 'size': im.size, 'mode': 'full', } try: while True: if im.tile: tile = im.tile[0] update_region = tile[1] update_region_dimensions = update_region[2:] if update_region_dimensions != im.size: results['mode'] = 'partial' break im.seek(im.tell() + 1) except EOFError: pass return results def processImage(path): ''' Iterate the GIF, extracting each frame. ''' mode = analyseImage(path)['mode'] im = Image.open(path) i = 0 p = im.getpalette() last_frame = im.convert('RGBA') try: while True: print "saving %s (%s) frame %d, %s %s" % (path, mode, i, im.size, im.tile) ''' If the GIF uses local colour tables, each frame will have its own palette. If not, we need to apply the global palette to the new frame. ''' if not im.getpalette(): im.putpalette(p) new_frame = Image.new('RGBA', im.size) ''' Is this file a "partial"-mode GIF where frames update a region of a different size to the entire image? If so, we need to construct the new frame by pasting it on top of the preceding frames. ''' if mode == 'partial': new_frame.paste(last_frame) new_frame.paste(im, (0,0), im.convert('RGBA')) new_frame.save('%s-%d.png' % (''.join(os.path.basename(path).split('.')[:-1]), i), 'PNG') i += 1 last_frame = new_frame im.seek(im.tell() + 1) except EOFError: pass def main(): processImage('test_gif.gif') if __name__ == "__main__": main()
当前标题:Python图像处理之gif动态图的解析与合成操作详解-创新互联
网页地址:http://myzitong.com/article/pepcj.html