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


猜你喜欢
- 目录概述语法定义接口实现接口空接口接口的组合总结概述Go 语言中的接口就是方法签名的集合,接口只有声明,没有实现,不包含变量。语法定义接口t
- 本文实例讲述了php打包压缩文件之ZipArchive方法用法。分享给大家供大家参考,具体如下:前面说到了php打包压缩文件之PclZip方
- 本文实例为大家分享了Python人脸识别的具体代码,供大家参考,具体内容如下1.利用opencv库sudo apt-get install
- 本例设置为垂直左侧scroll主要思想是利用一个长度为0的mid_frame,高度为待设置qwidget的高度,用mid_frame的mov
- 引言之前有些无聊(呆在家里实在玩的腻了),然后就去B站看了一些python爬虫视频,没有进行基础的理论学习,也就是直接开始实战,感觉跟背公式
- Rect(rectangle)指的是矩形,或者长方形,在 Pygame 中我们使用 Rect() 方法来创建一个指定位置,大小的矩形区域。函
- 使用matplotlib绘图时,在弹出的窗口中默认是有工具栏的,那么这些工具栏是如何定义的呢?工具栏的三种模式matplotlib的基础配置
- 本文实例讲述了python 并发下载器实现方法。分享给大家供大家参考,具体如下:并发下载器并发下载原理from gevent import
- 假设名为A.py的文件需要调用B.py文件内的C(x,y)函数假如在同一目录下,则只需import Bif __name__ == &quo
- blob对象介绍一个 Blob对象表示一个不可变的, 原始数据的类似文件对象。Blob表示的数据不一定是一个JavaScript原生格式 b
- 目录简介快速使用格式时区cli总结参考简介不管什么时候,处理时间总是让人头疼的一件事情。因为时间格式太多样化了,再加上时区,夏令时,闰秒这些
- 一、资料定义 ddl(data definition language) 资料定语言是指对资料的格
- 花了些工夫将碎片网部署到了SAE,中途遇到各类问题。感觉SAE看上去很美,实际上却并不是太成熟(至少python版如此)。下面记录下我遇到的
- 今天以一个表单的自动提交,来进一步学习selenium的用法练习目标0)运用selenium启动firefox并载入指定页面(这部分可查看本
- 游戏截图动态演示源码分享state/tool.pyimport osimport jsonfrom abc import abstractm
- 常用的python第三方库安装工具大概有三种:1、pip (推荐)2、easy_install3、setup.py常见的安装包格式:1、wh
- 远程运行最怕断电,训练了几个小时的数据说没就没,或者停止运行。用nohup 记录代码的输出,还可以不受断电的影响。方法1. 用nohup 运
- 1.简单检索数据博客内容中student表为:1.1.检索单个列select + 列名 + from + 表名1.2.检索多个列select
- 区别:xx:公有变量,所有对象都可以访问;xxx:双下划线代表着是系统定义的名字。__xxx:双前置下划线,避免与子类中的属性命名冲突,无法
- python读取npz/npy文件npz和npy文件都可以直接使用numpy读写。import numpy as npac = np.loa