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三大器之迭代器、生成器、装饰器

    python三大器之迭代器、生成器、装饰器

    迭代是Python最强大的功能之一,是访问集合元素的一种方式;迭代器是一个可以记住遍历的位置的对象,本文给大家介绍python三大器之迭代器、生成器、装饰器的相关知识,感兴趣的朋友跟随小编一起看看吧
    2022-01-01
  • 使用 Python 清理收藏夹里已失效的网站

    使用 Python 清理收藏夹里已失效的网站

    这篇文章主要介绍了用 Python 清理收藏夹里已失效的网站,本文通过截图实例代码的形式给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12
  • 一文了解python 3 字符串格式化 F-string 用法

    一文了解python 3 字符串格式化 F-string 用法

    本文介绍在python 3 编程中,如何进行字符串格式化。介绍了F-string的用法,通过实例代码给大家介绍的非常详细,对大家的工作或学习具有一定的参考借鉴价值,需要的朋友参考下吧
    2020-03-03
  • win10安装python3.6的常见问题

    win10安装python3.6的常见问题

    在本篇文章里小编给大家分享的是关于win10安装python3.6的具体步骤,有兴趣的朋友们可以参考学习下。
    2020-07-07
  • 详解Python中for循环的使用方法

    详解Python中for循环的使用方法

    这篇文章主要介绍了Python中for循环的使用方法,是Python入门中的基础知识,需要的朋友可以参考下
    2015-05-05
  • Django migrate报错的解决方案

    Django migrate报错的解决方案

    在讲解如何解决migrate报错原因前,我们先要了解migrate做了什么事情,本文就详细的介绍migrate使用以及出现问题的解决,感兴趣的可以了解一下
    2021-05-05
  • python解压zip包中文乱码解决方法

    python解压zip包中文乱码解决方法

    这篇文章主要介绍了python解压zip包中文乱码解决方法,帮助大家更好的理解和学习python,感兴趣的朋友可以了解下
    2020-11-11
  • 利用Python-iGraph如何绘制贴吧/微博的好友关系图详解

    利用Python-iGraph如何绘制贴吧/微博的好友关系图详解

    这篇文章主要给大家介绍了关于利用Python-iGraph如何绘制贴吧/微博好友关系图的相关资料,文中显示介绍了在windows系统下安装python-igraph的步骤,然后通过示例代码演示了绘制好友关系图的方法,需要的朋友可以参考下。
    2017-11-11
  • python ES连接服务器的方法详解

    python ES连接服务器的方法详解

    使用Python连接Elasticsearch服务器进行数据搜索和分析是一项常见操作,本文详细介绍了如何使用elasticsearch-py客户端库连接到Elasticsearch服务器,并执行创建索引、添加文档及搜索等基本操作
    2024-10-10
  • Python+folium绘制精美地图的示例详解

    Python+folium绘制精美地图的示例详解

    folium是一个基于leaflet.js的python地图库,可以通过folium来操纵数据,并将其可视化。本文将通过各种示例详细讲解如何利用folium绘制精美地图,需要的可以参考一下
    2022-03-03

最新评论