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处理RSS、ATOM模块FEEDPARSER介绍
Feb 18 Python
Django 视图层(view)的使用
Nov 09 Python
python读取目录下最新的文件夹方法
Dec 24 Python
python使用wxpy实现微信消息防撤回脚本
Apr 29 Python
pandas的qcut()方法详解
Jul 06 Python
用python实现英文字母和相应序数转换的方法
Sep 18 Python
python获取引用对象的个数方式
Dec 20 Python
python scatter函数用法实例详解
Feb 11 Python
基于Python共轭梯度法与最速下降法之间的对比
Apr 02 Python
Python 利用Entrez库筛选下载PubMed文献摘要的示例
Nov 24 Python
Django用内置方法实现简单搜索功能的方法
Dec 18 Python
python中的unittest框架实例详解
Feb 05 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
动漫定律:眯眯眼都是怪物!这些角色狠话不多~
2020/03/03 日漫
PHP系统流量分析的程序
2006/10/09 PHP
一个PHP日历程序
2006/12/06 PHP
PHP Mysql编程之高级技巧
2008/08/27 PHP
php的zip解压缩类pclzip使用示例
2014/03/14 PHP
php中有关字符串的4个函数substr、strrchr、strstr、ereg介绍和使用例子
2014/04/24 PHP
destoon安装出现Internal Server Error的解决方法
2014/06/21 PHP
PHP SplObjectStorage使用实例
2015/05/12 PHP
一个简单至极的PHP缓存类代码
2015/10/23 PHP
php简单处理XML数据的方法示例
2017/05/19 PHP
用JavaScript事件串连执行多个处理过程的方法
2007/03/09 Javascript
jQuery中live方法的重复绑定说明
2011/10/21 Javascript
jquery实现表单验证简单实例演示
2015/11/23 Javascript
jQuery实现伪分页的方法分享
2016/02/17 Javascript
js HTML5多图片上传及预览实例解析(不含前端的文件分割)
2016/08/26 Javascript
JavaScript实现url参数转成json形式
2016/09/25 Javascript
Ajax与服务器(JSON)通信实例代码
2016/11/05 Javascript
jquery将标签元素的高设为屏幕的百分比
2017/04/19 jQuery
Javascript别踩白块儿(钢琴块儿)小游戏实现代码
2017/07/20 Javascript
用p5.js制作烟花特效的示例代码
2018/03/21 Javascript
vue.js使用watch监听路由变化的方法
2018/07/08 Javascript
vue组件表单数据回显验证及提交的实例代码
2018/08/30 Javascript
vue项目从node8.x升级到12.x后的问题解决
2019/10/25 Javascript
python网络编程学习笔记(一)
2014/06/09 Python
TensorFlow实现iris数据集线性回归
2018/09/07 Python
django的模型类管理器——数据库操作的封装详解
2020/04/01 Python
Python中实现输入一个整数的案例
2020/05/03 Python
基于 HTML5 Canvas实现 的交互式地铁线路图
2018/03/05 HTML / CSS
HTML5 解析规则分析
2009/08/14 HTML / CSS
移动端HTML5 input常见问题(小结)
2020/09/28 HTML / CSS
Penhaligon’s英国官网:成立于1870年的英国香水制造商
2021/02/18 全球购物
Linux内核的同步机制是什么?主要有哪几种内核锁
2013/01/03 面试题
优秀教师先进事迹
2014/01/22 职场文书
质量月口号
2014/06/20 职场文书
2014年度培训工作总结
2014/11/27 职场文书
南京南京观后感
2015/06/02 职场文书