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基础教程之基本数据类型和变量声明介绍
Aug 29 Python
剖析Python的Tornado框架中session支持的实现代码
Aug 21 Python
python使用pandas实现数据分割实例代码
Jan 25 Python
使用python存储网页上的图片实例
May 22 Python
对python dataframe逻辑取值的方法详解
Jan 30 Python
django 使用全局搜索功能的实例详解
Jul 18 Python
Python3打包exe代码2种方法实例解析
Feb 17 Python
利用python绘制数据曲线图的实现
Apr 09 Python
TensorFlow tf.nn.conv2d_transpose是怎样实现反卷积的
Apr 20 Python
Python使用matplotlib绘制圆形代码实例
May 27 Python
python 如何快速复制序列
Sep 07 Python
教你使用Python pypinyin库实现汉字转拼音
May 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
深入PHP5中的魔术方法详解
2013/06/17 PHP
php输入流php://input使用示例(php发送图片流到服务器)
2013/12/25 PHP
PHP框架Laravel的小技巧两则
2015/02/10 PHP
php简单判断文本编码的方法
2015/07/30 PHP
JavaScript中使用正则匹配多条,且获取每条中的分组数据
2010/11/30 Javascript
JS实现简单的Canvas画图实例
2013/07/04 Javascript
一个非常全面的javascript URL解析函数和分段URL解析方法
2014/04/12 Javascript
nodejs 图解express+supervisor+ejs的用法(推荐)
2017/09/08 NodeJs
JS+jQuery实现注册信息的验证功能
2017/09/26 jQuery
微信公众平台 客服接口发消息的实现代码(Java接口开发)
2019/04/17 Javascript
ES2020 新特性(种草)
2020/01/12 Javascript
vuex实现购物车的增加减少移除
2020/06/28 Javascript
vue中keep-alive、activated的探讨和使用详解
2020/07/26 Javascript
在antd Form表单中select设置初始值操作
2020/11/02 Javascript
[40:27]完美世界DOTA2联赛PWL S3 PXG vs GXR 第一场 12.19
2020/12/24 DOTA
python获取指定目录下所有文件名列表的方法
2015/05/20 Python
Python中生成器和迭代器的区别详解
2018/02/10 Python
用python实现百度翻译的示例代码
2018/03/09 Python
详解Appium+Python之生成html测试报告
2019/01/04 Python
python对象转字典的两种实现方式示例
2019/11/07 Python
python实现图片插入文字
2019/11/26 Python
python给指定csv表格中的联系人群发邮件(带附件的邮件)
2019/12/31 Python
pytorch中的自定义数据处理详解
2020/01/06 Python
pytorch快速搭建神经网络_Sequential操作
2020/06/17 Python
布里斯班女装时尚品牌:Adrift
2017/12/28 全球购物
外贸英语专业求职信范文
2013/12/25 职场文书
导游的职业规划书范文
2013/12/27 职场文书
军训心得体会
2013/12/31 职场文书
物流毕业生个人的自我评价
2014/02/13 职场文书
幼儿园校园小喇叭广播稿
2014/10/17 职场文书
六年级语文下册教学计划
2015/01/22 职场文书
工会经费申请报告
2015/05/15 职场文书
观看焦裕禄观后感
2015/06/09 职场文书
商业计划书如何写?关键问题有哪些?
2019/07/11 职场文书
详解Nginx 工作原理
2021/03/31 Servers
MySQL 四种连接和多表查询详解
2021/07/16 MySQL