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


Posted in Python onDecember 30, 2018

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

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

一、gif动态图的合成

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

Python图像处理之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张相同分辨率的静态图组合而成

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

二、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图像处理之gif动态图的解析与合成操作详解

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

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

Python 相关文章推荐
Python数组遍历的简单实现方法小结
Apr 27 Python
python爬虫实现教程转换成 PDF 电子书
Feb 19 Python
Python模拟登陆实现代码
Jun 14 Python
Python时间的精准正则匹配方法分析
Aug 17 Python
mac下给python3安装requests库和scrapy库的实例
Jun 13 Python
Python 输出时去掉列表元组外面的方括号与圆括号的方法
Dec 24 Python
使用Python实现将list中的每一项的首字母大写
Jun 11 Python
django rest framework vue 实现用户登录详解
Jul 29 Python
python 修改本地网络配置的方法
Aug 14 Python
Python运行异常管理解决方案
Mar 09 Python
Python-jenkins 获取job构建信息方式
May 12 Python
Python django框架 web端视频加密的实例详解
Nov 20 Python
python爬虫获取小区经纬度以及结构化地址
Dec 30 #Python
python实现播放音频和录音功能示例代码
Dec 30 #Python
利用python实现简易版的贪吃蛇游戏(面向python小白)
Dec 30 #Python
python中partial()基础用法说明
Dec 30 #Python
python读取各种文件数据方法解析
Dec 29 #Python
python 读取鼠标点击坐标的实例
Dec 29 #Python
对python for 文件指定行读写操作详解
Dec 29 #Python
You might like
UCenter Home二次开发指南
2009/05/28 PHP
PHP递归算法的详细示例分析
2013/02/19 PHP
php实现的短网址算法分享
2014/06/20 PHP
如何让thinkphp在模型中自动完成session赋值小教程
2014/09/05 PHP
PHP实现的无限分类类库定义与用法示例【基于thinkPHP】
2018/08/06 PHP
Javascript insertAfter() 实现函数代码
2011/10/12 Javascript
js 三级关联菜单效果实例
2013/08/13 Javascript
javascript实现的弹出层背景置灰-模拟(easyui dialog)
2013/12/27 Javascript
jQuery中:checkbox选择器用法实例
2015/01/03 Javascript
vue项目中axios使用详解
2018/02/07 Javascript
微信小程序 image组件遇到的问题
2019/05/28 Javascript
对layer弹出框中icon数字参数的说明介绍
2019/09/04 Javascript
微信小程序 行的删除和增加操作实现详解
2019/09/29 Javascript
layui 弹出层值回传解决方式
2019/11/14 Javascript
VueJS实现用户管理系统
2020/05/29 Javascript
解决idea开发遇到javascript动态添加html元素时中文乱码的问题
2020/09/29 Javascript
[02:25]DOTA2英雄基础教程 虚空假面
2014/01/02 DOTA
Python运用于数据分析的简单教程
2015/03/27 Python
Python中内置数据类型list,tuple,dict,set的区别和用法
2015/12/14 Python
python自动翻译实现方法
2016/05/28 Python
python2.7的编码问题与解决方法
2016/10/04 Python
浅谈Python2获取中文文件名的编码问题
2018/01/09 Python
关于python中的xpath解析定位
2020/03/06 Python
一款CSS3实现多功能下拉菜单(带分享按)的教程
2014/11/05 HTML / CSS
博士学位自我鉴定范文
2013/12/26 职场文书
《黄山奇石》教学反思
2014/04/19 职场文书
校园文化标语
2014/06/18 职场文书
校长创先争优承诺书
2014/08/30 职场文书
国际残疾人日广播稿范文
2014/10/09 职场文书
2015公务员试用期工作总结
2014/12/12 职场文书
投资意向协议书
2015/01/29 职场文书
销售经理工作检讨书
2015/02/19 职场文书
图书馆义工感想
2015/08/07 职场文书
党员干部学习心得体会
2016/01/23 职场文书
MySQL完整性约束的定义与实例教程
2021/05/30 MySQL
DSP接收机前端设想
2022/04/05 无线电