Python图像处理之gif动态图的解析与合成操作详解
作者:PHILOS_THU 发布时间:2022-09-27 18:19:51
标签:Python,图像处理,gif
本文实例讲述了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动态图的合成
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程序设计有所帮助。
来源:https://blog.csdn.net/guduruyu/article/details/77540445
0
投稿
猜你喜欢
- 本文实例讲述了python实现生成Word、docx文件的方法。分享给大家供大家参考,具体如下:http://python-docx.rea
- 本文实例总结了Python中字符串的常见操作技巧。分享给大家供大家参考,具体如下:反转一个字符串>>> S = '
- 使用标准库importlib的import_module()函数、django的import_string(),它们都可以动态加载指定的 P
- 引言语音端点检测最早应用于电话传输和检测系统当中,用于通信信道的时间分配,提高传输线路的利用效率.端点检测属于语音处理系统的前端操作,在语音
- 一、桌面应用软件桌面应用软件是基于GUI(Graphical User Interface,图形用户界面)交互式程序,需要实现GUI库实现前
- (asp.net的应用). 在网上一搜有很多此类文章,但我需要将公司的复杂的,较大的web应用也以此方式操作,比较的头大。一般的文章建议将b
- 在web应用中,常常会遇见点击某个链接会弹出一个新的窗口,或者是相互关联的web应用 ,这样要去操作新窗口中的元素,这时就需要主机切换到新窗
- RPC是Remote Procedure Call的缩写,翻译成中文就是远程方法调用,是一种在本地的机器上调用远端机器上的一个过程(方法)的
- 当我们在使用pycharm时,输入特殊的关键字会有提示,然后按enter就可以自动补全,如果我们经常需要输出重复的代码时,能否也利用这种方法
- 简易网页搜集器前面我们已经学会了简单爬取浏览器页面的爬虫。但事实上我们的需求当然不是爬取搜狗首页或是B站首页这么简单,再不济,我们都希望可以
- 一、什么是模块容器 -> 数据的封装函数 -> 语句的封装类 -> 方法和属性的封装模块 -> 模块就是程序,模块就
- requests库安装和导入第一步:cmd打开命令行,使用如下命令安装requests库。pip install requests由于我的安
- 疫情肆虐,憋在家实在无聊,索性写点东西,于是就有了这个极极极极极简的音乐播放器。这个极极极简的音乐播放器类似于“阅后即焚”的软件,播放器可以
- function.js<!--function getObject(objectId) { &nbs
- python和PHP相比较,python适合做爬虫。原因如下抓取网页本身的接口相比与其他静态编程语言,如java,c#,C++,python
- 本文研究的是re模块findall()函数的相关内容,首先看看实例代码:>>> import re >>>
- MySQL出错代码列表:1005:创建表失败1006:创建数据库失败1007:数据库已存在,创建数据库失败1008:数据库不存在,删除数据库
- 本文主要探索的是python的Crypto模块实现AES加密,分享了具体实现代码,下面看看具体内容。学了使用Crypto模块的AES来加密文
- 关键字:js验证表单大全,用JS控制表单提交 ,javascript提交表单:目录:1:js 字符串长度限制、判断字符长度 、js限制输入、
- 1.自定义线程池import threadingimport Queueimport timequeue = Queue.Queue()de