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 08 Python
Python实现的HMacMD5加密算法示例
Apr 03 Python
获取python的list中含有重复值的index方法
Jun 27 Python
win7 x64系统中安装Scrapy的方法
Nov 18 Python
python简单验证码识别的实现方法
May 10 Python
python占位符输入方式实例
May 27 Python
python基于json文件实现的gearman任务自动重启代码实例
Aug 13 Python
Python版中国省市经纬度
Feb 11 Python
Python编写memcached启动脚本代码实例
Aug 14 Python
Django ModelForm组件原理及用法详解
Oct 12 Python
python切割图片的示例
Nov 12 Python
pandas使用函数批量处理数据(map、apply、applymap)
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
使用Curl进行抓取远程内容时url中文编码问题示例探讨
2013/10/29 PHP
php几个预定义变量$_SERVER用法小结
2014/11/07 PHP
Linux php 中文乱码的快速解决方法
2016/05/13 PHP
PHP图片添加水印功能示例小结
2016/10/03 PHP
漂亮的widgets,支持换肤和后期开发新皮肤
2007/04/23 Javascript
网站导致浏览器崩溃的原因总结(多款浏览器) 推荐
2010/04/15 Javascript
JS中实现replaceAll的方法(实例代码)
2013/11/12 Javascript
js实现遮罩层划出效果是生成div而不是显示
2014/07/29 Javascript
原生Ajax 和jQuery Ajax的区别示例分析
2014/12/17 Javascript
JS获取iframe中longdesc属性的方法
2015/04/01 Javascript
JavaScript String 对象常用方法总结
2016/04/28 Javascript
jQuery日程管理插件fullcalendar使用详解
2017/01/07 Javascript
用node和express连接mysql实现登录注册的实现代码
2017/07/05 Javascript
实现div滚动条默认最底部以及默认最右边的示例代码
2017/11/15 Javascript
Express下采用bcryptjs进行密码加密的方法
2018/02/07 Javascript
JavaScript"模拟事件"的注意要点详解
2019/02/13 Javascript
Vue拖拽组件列表实现动态页面配置功能
2019/06/17 Javascript
python获取文件扩展名的方法
2015/07/06 Python
python删除指定类型(或非指定)的文件实例详解
2015/07/06 Python
python实现文件路径和url相互转换的方法
2015/07/06 Python
python同义词替换的实现(jieba分词)
2020/01/21 Python
哪些是python中web开发框架
2020/06/17 Python
python操作链表的示例代码
2020/09/27 Python
详解pycharm配置python解释器的问题
2020/10/15 Python
英国儿童家具专卖店:GLTC
2016/09/24 全球购物
万代美国官网:PREMIUM BANDAI USA
2020/09/11 全球购物
院药学专业个人求职信
2013/09/21 职场文书
销售人员中英文自荐信
2013/09/22 职场文书
总经理驾驶员岗位职责
2013/12/04 职场文书
霸气押韵的班级口号
2014/06/09 职场文书
干部对照检查材料范文
2014/08/26 职场文书
计算机实训报告范文
2014/11/05 职场文书
党员个人承诺书
2015/04/27 职场文书
Python批量将csv文件转化成xml文件的实例
2021/05/10 Python
JS实现数组去重的11种方法总结
2022/04/04 Javascript
Nginx动静分离配置实现与说明
2022/04/07 Servers