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中的字典详细介绍
Sep 18 Python
MySQL中表的复制以及大型数据表的备份教程
Nov 25 Python
win7上python2.7连接mysql数据库的方法
Jan 14 Python
详解用python实现简单的遗传算法
Jan 02 Python
python遍历文件夹下所有excel文件
Jan 03 Python
python实现比对美团接口返回数据和本地mongo数据是否一致示例
Aug 09 Python
Python利用matplotlib绘制约数个数统计图示例
Nov 26 Python
python PIL/cv2/base64相互转换实例
Jan 09 Python
彻底搞懂 python 中文乱码问题(深入分析)
Feb 28 Python
Anaconda配置pytorch-gpu虚拟环境的图文教程
Apr 16 Python
python如何实现读取并显示图片(不需要图形界面)
Jul 08 Python
Python 如何利用ffmpeg 处理视频素材
Nov 27 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
深入解析php模板技术原理【一】
2008/01/10 PHP
php中的三元运算符使用说明
2011/07/03 PHP
php读取mssql的ntext字段返回值为空的解决方法
2014/12/30 PHP
PHP的数组中提高元素查找与元素去重的效率的技巧解析
2016/03/03 PHP
php微信开发之音乐回复功能
2018/06/14 PHP
用jQuery扩展自写的 UI导航
2010/01/13 Javascript
jquery formValidator插件ajax验证 内容不做任何修改再离开提示错误的bug解决方法
2013/01/04 Javascript
一个jquery实现的不错的多行文字图片滚动效果
2014/09/28 Javascript
node.js中的fs.readFileSync方法使用说明
2014/12/15 Javascript
node.js中的favicon.ico请求问题处理
2014/12/15 Javascript
js实现网页图片延时加载 提升网页打开速度
2016/01/26 Javascript
基于JS实现数字+字母+中文的混合排序方法
2016/06/06 Javascript
JavaScript实现星级评分
2017/01/12 Javascript
jQuery实现动态生成表格并为行绑定单击变色动作的方法
2017/04/17 jQuery
ES6入门教程之Class和Module详解
2017/05/17 Javascript
element-ui 上传图片后清空图片显示的实例
2018/09/04 Javascript
使用vue.js在页面内组件监听scroll事件的方法
2018/09/11 Javascript
Ant Design Pro 下实现文件下载的实现代码
2019/12/03 Javascript
js实现鼠标点击飘爱心效果
2020/08/19 Javascript
浅谈vue使用axios的回调函数中this不指向vue实例,为undefined
2020/09/21 Javascript
Python字符串逐字符或逐词反转方法
2015/05/21 Python
使用Python来开发Markdown脚本扩展的实例分享
2016/03/04 Python
Python程序中设置HTTP代理
2016/11/06 Python
windows上安装Anaconda和python的教程详解
2017/03/28 Python
基于python实现简单日历
2018/07/28 Python
Python中关键字global和nonlocal的区别详解
2018/09/03 Python
Django ORM多对多查询方法(自定义第三张表&ManyToManyField)
2019/08/09 Python
Python 调用 Windows API COM 新法
2019/08/22 Python
Python 中pandas索引切片读取数据缺失数据处理问题
2019/10/09 Python
泰国汽车、火车和轮渡票预订网站:Bus Online Ticket
2017/09/09 全球购物
Wedgwood美国官网:英国骨瓷,精美礼品及家居装饰
2018/02/17 全球购物
Clarks西班牙官方在线商店:clarks鞋
2019/05/03 全球购物
Jar包的作用是什么
2014/03/30 面试题
大三学习计划书范文
2014/05/02 职场文书
入团申请书格式
2019/06/20 职场文书
Win11 Build 22000.829更新补丁KB5015882发布(附更新修复内容汇总)
2022/07/15 数码科技