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系列之数据处理编程实例(二)
May 22 Python
Python标准库urllib2的一些使用细节总结
Mar 16 Python
Python实现快速排序算法及去重的快速排序的简单示例
Jun 26 Python
caffe binaryproto 与 npy相互转换的实例讲解
Jul 09 Python
django 自定义过滤器的实现
Feb 26 Python
如何基于Python批量下载音乐
Nov 11 Python
Python线程障碍对象Barrier原理详解
Dec 02 Python
从训练好的tensorflow模型中打印训练变量实例
Jan 20 Python
解决Python logging模块无法正常输出日志的问题
Feb 21 Python
python由已知数组快速生成新数组的方法
Apr 08 Python
利用Python实现Excel的文件间的数据匹配功能
Jun 16 Python
pip/anaconda修改镜像源,加快python模块安装速度的操作
Mar 04 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 socke 向指定页面提交数据
2008/07/23 PHP
windows环境下php配置memcache的具体操作步骤
2013/06/09 PHP
PHP使用pcntl_fork实现多进程下载图片的方法
2014/12/16 PHP
PHP 爬取网页的主要方法
2018/07/13 PHP
php实现记事本案例
2020/10/20 PHP
10个基于jQuery或JavaScript的WYSIWYG 编辑器整理
2010/05/06 Javascript
jquery提升性能最佳实践小结
2010/12/06 Javascript
javascript图片延迟加载实现方法及思路
2015/12/31 Javascript
PHP+jquery+ajax实现分页
2016/12/09 Javascript
JQuery EasyUI 结合ztrIee的后台页面开发实例
2017/09/01 jQuery
详解JSONObject和JSONArray区别及基本用法
2017/10/25 Javascript
vue实现codemirror代码编辑器中的SQL代码格式化功能
2019/08/27 Javascript
使用zrender.js绘制体温单效果
2019/10/31 Javascript
javascript json对象小技巧之键名作为变量用法分析
2019/11/11 Javascript
[55:48]VGJ.S vs TNC Supermajor 败者组 BO3 第二场 6.6
2018/06/07 DOTA
[02:19]2018年度DOTA2最佳核心位选手-完美盛典
2018/12/17 DOTA
详解python中asyncio模块
2018/03/03 Python
python 巧用正则寻找字符串中的特定字符的位置方法
2018/05/02 Python
Django网络框架之创建虚拟开发环境操作示例
2019/06/06 Python
Python调用graphviz绘制结构化图形网络示例
2019/11/22 Python
英国最大的宠物食品和宠物用品网上零售商: Zooplus
2016/08/01 全球购物
系统管理员的职责包括那些?管理的对象是什么?
2016/09/20 面试题
如何用Python输出一个Fibonacci数列
2016/08/28 面试题
公司前台接待岗位职责
2013/12/03 职场文书
自荐信封面
2013/12/04 职场文书
面包店的创业计划书范文
2014/01/16 职场文书
财务会计毕业生个人求职信
2014/02/03 职场文书
《乡愁》教学反思
2014/02/18 职场文书
运动会加油稿100字
2014/09/19 职场文书
2014年政务公开工作总结
2014/12/09 职场文书
优秀班主任事迹材料
2014/12/16 职场文书
给下属加薪申请报告
2015/05/15 职场文书
简历自我评价范文
2019/04/24 职场文书
Python基础详解之描述符
2021/04/28 Python
Spring mvc是如何实现与数据库的前后端的连接操作的?
2021/06/30 Java/Android
MySQL中JOIN连接的基本用法实例
2022/06/05 MySQL