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实现文件路径和url相互转换的方法
Jul 06 Python
使用Python爬取最好大学网大学排名
Feb 24 Python
Tensorflow卷积神经网络实例
May 24 Python
python3学生名片管理v2.0版
Nov 29 Python
Pycharm 实现下一个文件引用另外一个文件的方法
Jan 17 Python
python随机在一张图像上截取任意大小图片的方法
Jan 24 Python
python 反编译exe文件为py文件的实例代码
Jun 27 Python
Python字典对象实现原理详解
Jul 01 Python
如何导出python安装的所有模块名称和版本号到文件中
Jun 05 Python
Python如何给你的程序做性能测试
Jul 29 Python
Python 中Operator模块的使用
Jan 30 Python
深入探讨opencv图像矫正算法实战
May 21 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下通过POST还是GET来传值
2008/06/05 PHP
PHP 危险函数解释 分析
2009/04/22 PHP
Laravel中的chunk组块结果集处理与注意问题
2018/08/15 PHP
PHP7实现和CryptoJS的AES加密方式互通示例【AES-128-ECB加密】
2019/06/08 PHP
使用swoole 定时器变更超时未支付订单状态的解决方案
2019/07/24 PHP
纯Javascript实现Windows 8 Metro风格实现
2013/10/15 Javascript
JS辨别访问浏览器判断是android还是ios系统
2014/08/19 Javascript
深入理解JavaScript系列(43):设计模式之状态模式详解
2015/03/04 Javascript
javascript实现数组去重的多种方法
2016/03/14 Javascript
JavaScript中常用的验证reg
2016/10/13 Javascript
实例分析浏览器中“JavaScript解析器”的工作原理
2016/12/12 Javascript
基于jQuery解决ios10以上版本缩放问题
2017/11/03 jQuery
layui 阻止图片上传的实例(before方法)
2019/09/26 Javascript
JavaScript语法约定和程序调试原理解析
2020/11/03 Javascript
vue3.0中setup使用(两种用法)
2020/12/02 Vue.js
[01:20:30]OG vs LGD 2018国际邀请赛淘汰赛BO3 第四场 8.26
2018/08/30 DOTA
Pycharm使用之设置代码字体大小和颜色主题的教程
2019/07/12 Python
python3.8与pyinstaller冲突问题的快速解决方法
2020/01/16 Python
python用pip install时安装失败的一系列问题及解决方法
2020/02/24 Python
python实现人性化显示金额数字实例详解
2020/09/25 Python
Myprotein台湾官方网站:全球领先的运动营养品牌
2018/12/10 全球购物
瑞典度假品牌:OAS
2019/05/28 全球购物
Simons官方网站:加拿大时尚零售商
2020/02/20 全球购物
牛津在线药房:Oxford Online Pharmacy
2020/11/16 全球购物
PHP解析URL是哪个函数?怎么用?
2013/05/09 面试题
数控技术应用个人求职信范文
2014/02/03 职场文书
企业晚会策划方案
2014/05/29 职场文书
离职证明范本(5篇)
2014/09/19 职场文书
2014年英语教师工作总结
2014/12/03 职场文书
三年级学生评语大全
2014/12/26 职场文书
法院个人总结
2015/03/03 职场文书
大学生安全教育主题班会
2015/08/12 职场文书
有关浪费资源的建议书
2015/09/14 职场文书
idea 在springboot中使用lombok插件的方法
2021/08/02 Java/Android
深入讲解数据库中Decimal类型的使用以及实现方法
2022/02/15 MySQL
Dashboard管理Kubernetes集群与API访问配置
2022/04/01 Servers