本文实例讲述了Python图像处理之gif动态图的解析与合成操作。分享给大家供大家参考,具体如下:

gif动态图是在现在已经司空见惯,朋友圈里也经常是一言不合就斗图。这里,就介绍下如何使用python来解析和生成gif图像。

一、gif动态图的合成

如下图,是一个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张相同分辨率的静态图组合而成

二、gif动态图的合成

代码如下:

#-*- 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程序设计有所帮助。

python合成gif动图_Python图像处理之gif动态图的解析与合成操作详解相关推荐

  1. python怎么存为动图_Python将视频或者动态图gif逐帧保存为图片的方法

    本文是基于opencv将视频和动态图gif保存为图像帧.可以根据输入视频格式的不同,修改第21行. 对动图的处理不同于视频,PIL库包含对图像序列的基本支持.当打开gif图像时,自动加载第一帧.当图像 ...

  2. python符号格式化设置区间_Python 数值区间处理_对interval 库的快速入门详解

    使用 Python 进行数据处理的时候,常常会遇到判断一个数是否在一个区间内的操作.我们可以使用 if else 进行判断,但是,既然使用了 Python,那我们当然是想找一下有没有现成的轮子可以用. ...

  3. python中的json函数_python中装饰器、内置函数、json的详解

    装饰器 装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象. 先看简单例子: def run(): time.sleep(1 ...

  4. 用python画动图_Python使用matplotlib画动态图

    机器学习需要使用python实现相应的算法,因此学习了Matplotlib中的画图. 当然为了能显示机器学习中每次迭代的效果与收敛速度,需要画出动态图形. 下面给出两个例子,分别可以画出动态条形图和动 ...

  5. python中x y表示_Python中表达式x += y和x = x+y 的区别详解

    前言 本文主要给大家介绍的是关于Python中表达式x += y和x = x+y 区别的相关内容,分享出来供大家参考学习,下面来看看详细的介绍: 直接看下面代码: x +=y In [66]: id( ...

  6. python获取当前线程名称_python使用标准库根据进程名如何获取进程的pid详解

    前言 标准库是Python的一个组成部分.这些标准库是Python为你准备好的利器,可以让编程事半功倍.特别是有时候需要获取进程的pid,但又无法使用第三方库的时候.下面话不多说了,来一起看看详细的介 ...

  7. python离线录音转文字_Python将文字转成语音并读出来的实例详解

    前言 本篇文章主要介绍,如何利用Python来实现将文字转成语音.将文字转成语音主要有两种不同的实现方法:先将文字转成语音,然后再通过读取语音实现发音.直接调用系统内置的语音引擎实现发音,后一种方法的 ...

  8. python中主函数循环_python入门(三):分支、循环、函数详解

    1.分支 if循环格式: if condition_1: statement_block_1 elif condition_2: statement_block_2 else: statement_b ...

  9. python中函数的调用_python函数的调用、函数中变量的使用之详解

    '''函数的工作原理:函数内部的变量都是临时的, 当你的函数返回以后,返回值可以被赋予一个变量. 这里是创建了一个新变量,用来存放函数的返回值. ''' def secret_formula(star ...

最新文章

  1. 结构光测距相位差_ROHM确立新型VCSEL模块技术 有助于提高测距精度
  2. Ext Scheduler Web资源甘特图控件
  3. 受益一生的15个学习习惯
  4. [译] 提高日志质量的 5 大技巧
  5. Mysql5.7中子查询时order by与group by合用无效的解决办法
  6. 将数据库的0和1显示为jsp页面的是和否
  7. 脚本语言语言脚本语言:Shell , JavaScript、VBScript、Perl、PHP、Python、Ruby、Lua
  8. 2018 开源分布式中间件 DBLE 年报
  9. 我的世界光影mod怎么用_我的世界RTX beta版视频体验:仿佛打破了次元壁
  10. 批量word删除页眉页脚——VBS脚本,在office宏中运行即可
  11. 高中数学建模优秀论文_数学建模论文 高中数学建模经典例题
  12. 量子纠缠 计算机,计算机科学家利用量子纠缠系统,证实44年前的一个猜想是错误的...
  13. 项目 cg day09
  14. GMM 简介与 Stata 实现
  15. b-spline学习-系数计算及程序实践
  16. java输出数组中所有数字排列的集合
  17. 基金量化交易系统如何实现套利成功?
  18. 大型圆弧怎么处理_这种大圆弧一次成型,回弹怎么计算?低公差
  19. 关于前端代码埋点数据上报的实现
  20. uni-app学习笔记之判断当前用户是否拥有某角色和权限

热门文章

  1. RavenDB FS 安装使用 介绍
  2. 食物链 poj 1182
  3. flexpaper 背景色变化
  4. ubutu14.04无法使用sudo,也无法切换到root用户去解决问题怎么办?
  5. 解决github很慢的问题
  6. Java中List.remove报UnsupportedOperationException异常
  7. Xcode8 注释快捷键无效, 解决方案
  8. styled-components解决全局样式‘injectGlobal‘ 废除的问题
  9. Amqp整合com.rabbitmq.client.ShutdownSignalException: channel error; protocol method异常处理
  10. 解决python中文乱码的方法