Python圖像處理之gif動(dòng)態(tài)圖的解析與合成操作詳解
本文實(shí)例講述了Python圖像處理之gif動(dòng)態(tài)圖的解析與合成操作。分享給大家供大家參考,具體如下:
gif動(dòng)態(tài)圖是在現(xiàn)在已經(jīng)司空見慣,朋友圈里也經(jīng)常是一言不合就斗圖。這里,就介紹下如何使用python來解析和生成gif圖像。
一、gif動(dòng)態(tài)圖的合成
如下圖,是一個(gè)gif動(dòng)態(tài)圖。
gif動(dòng)態(tài)圖的解析可以使用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()
解析結(jié)果如下,由此可見改動(dòng)態(tài)圖實(shí)際上是由14張相同分辨率的靜態(tài)圖組合而成
二、gif動(dòng)態(tài)圖的合成
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幅圖,間副的間隔時(shí)間為0.1s,合成新的gif動(dòng)態(tài)圖如下:
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python實(shí)現(xiàn)登錄人人網(wǎng)并抓取新鮮事的方法
這篇文章主要介紹了Python實(shí)現(xiàn)登錄人人網(wǎng)并抓取新鮮事的方法,可實(shí)現(xiàn)Python模擬登陸并抓取新鮮事的功能,需要的朋友可以參考下2015-05-05利用Python實(shí)現(xiàn)最小二乘法與梯度下降算法
這篇文章主要介紹了利用Python實(shí)現(xiàn)最小二乘法與梯度下降算法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02python中利用Future對象異步返回結(jié)果示例代碼
future是一種對象,表示異步執(zhí)行的操作。下面這篇文章主要給大家介紹了關(guān)于python中利用Future對象異步返回結(jié)果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09