Python图像处理之gif动态图的解析与合成操作详解

 更新时间:2018年12月30日 15:01:55   作者:PHILOS_THU  
这篇文章主要介绍了Python图像处理之gif动态图的解析与合成操作,结合实例形式分析了Python基于PIL模块解析gif文件,以及基于imageio库合成gif文件的相关操作技巧,需要的朋友可以参考下

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

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()

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

二、gif动态图的合成

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

代码如下:

#-*- coding: UTF-8 -*-
import imageio
def 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)
  return
def 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_list, gif_name)
if __name__ == "__main__":
  main()

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

更多关于Python相关内容可查看本站专题:《Python数学运算技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

  • python hook监听事件详解

    python hook监听事件详解

    这篇文章主要为大家详细介绍了python hook监听事件的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • python使用OpenCV模块实现图像的融合示例代码

    python使用OpenCV模块实现图像的融合示例代码

    这篇文章主要介绍了python使用OpenCV模块实现图像的融合示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • python eval()函数使用详情

    python eval()函数使用详情

    这篇文章主要来来聊聊python eval()函数使用方法本文将以python eval()函数使用方法来展开内容,需要的小伙伴可以参考以下文章的内容,希望对你有所帮助
    2021-10-10
  • django轻松使用富文本编辑器CKEditor的方法

    django轻松使用富文本编辑器CKEditor的方法

    最近由于需要在django admin中使用到富文本编辑器,由于我比较喜欢CKEditor富文本编辑器,于是就有了这篇文章,下面这篇文章主要给大家介绍了在django中轻松使用富文本编辑器CKEditor的方法,需要的朋友可以参考下。
    2017-03-03
  • Python+Pygame制作"长沙版"大富翁

    Python+Pygame制作"长沙版"大富翁

    说到童年爱玩的电脑游戏,最国民的莫过于金山打字通,接着是扫雷、红心大战,而红极一时的单机游戏当属《大富翁》。本文将通过Python的Pygame模块制作"长沙版"的大富翁,需要的可以参考一下
    2022-02-02
  • Python实现求两个csv文件交集的方法

    Python实现求两个csv文件交集的方法

    这篇文章主要介绍了Python实现求两个csv文件交集的方法,涉及Python针对csv文件的读取、遍历、判断等相关操作技巧,需要的朋友可以参考下
    2017-09-09
  • python3操作mysql数据库的方法

    python3操作mysql数据库的方法

    这篇文章主要介绍了python3操作mysql数据库的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Python Fuzzywuzzy库基本函数及模糊字符串匹配应用实战

    Python Fuzzywuzzy库基本函数及模糊字符串匹配应用实战

    fuzzywuzzy 是一个用于模糊字符串匹配的 Python 库,它基于编辑距离算法,提供了多个函数来比较字符串之间的相似性,在实际开发中,字符串匹配是一项常见但具有挑战性的任务,用户可能犯拼写错误,使用缩写或者输入同义词,因此,我们需要一种方法来处理这些情况
    2023-12-12
  • Python Socket实现简单TCP Server/client功能示例

    Python Socket实现简单TCP Server/client功能示例

    这篇文章主要介绍了Python Socket实现简单TCP Server/client功能,结合实例形式分析了Python基于socket创建TCP服务器Server与客户端client相关实现步骤与操作技巧,需要的朋友可以参考下
    2017-08-08
  • Python入门教程(二十三)Python的继承

    Python入门教程(二十三)Python的继承

    这篇文章主要介绍了Python入门教程(二十三)Python的继承,Python是一门非常强大好用的语言,也有着易上手的特性,本文为入门教程,需要的朋友可以参考下
    2023-04-04

最新评论