• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python处理gif动态图的解析与合成操作的介绍

python 搞代码 4年前 (2022-01-09) 18次浏览 已收录 0个评论

本篇文章给大家带来的内容是关于Python处理gif动态图的解析与合成操作的介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

本文实例讲述了Python图像处理之gif动态图的解析与合成操作。分享给大家供大家参考,具体如下:

gif动态图是在现在已经司空见惯,朋友圈里也经常是一言不合就斗图。这里,就介绍下如何使用python来解析和生成gif图像。

一、gif动态图的合成

如下图,是一个gif动态图。

gif动态图的解析可以使用PIL图像模块即可,具体代码如下:

#-*- coding: UTF-8 -*-import osfrom PIL import Imagedef 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 resultsdef 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:    passdef main():  processImage('test_gif.gif')if __name__ == "__main__":  main()

解析结果如下,由此可见改动态图实际上是由14张相同分辨率的静态图组合而成

二、gif动态图的合成

gif图像的合成,使用imageio库(https://pypi.python.org/pypi/imageio)

代码如下:

#-*- coding: UTF-8 -*-import imageiodef create_gif(image_list, gif_name):  frames = []  for image_name in image_list:    frames.append(imageio.imread(image_name))  # Save them as frames into a gif  imageio.mimsave(gif_name, frames, 'GIF', duration = 0.1)  returndef main():  image_list = ['test_gif-0.png', 'test_gif-2.png', 'test_gif-4.png',         'test_gif-6.png', 'test_gif-8.png', 'test_gif-10.png']  gif_name = 'created_gif.gif'  create_gif(image_l<p style="color:transparent">本文来源gao!daima.com搞$代!码网</p>ist, gif_name)if __name__ == "__main__":  main()

这里,使用第一步解析出来的图像中的8幅图,间副的间隔时间为0.1s,合成新的gif动态图如下:

以上就是Python处理gif动态图的解析与合成操作的介绍的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python处理gif动态图的解析与合成操作的介绍

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址