国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python圖像處理之gif動態圖的解析與合成操作詳解

2020-01-04 13:39:28
字體:
來源:轉載
供稿:網友

本文實例講述了Python圖像處理之gif動態圖的解析與合成操作。分享給大家供大家參考,具體如下:

gif動態圖是在現在已經司空見慣,朋友圈里也經常是一言不合就斗圖。這里,就介紹下如何使用python來解析和生成gif圖像。

一、gif動態圖的合成

如下圖,是一個gif動態圖。

Python,圖像處理之,gif,動態圖

gif動態圖的解析可以使用PIL圖像模塊即可,具體代碼如下:

#-*- coding: UTF-8 -*-import osfrom PIL import Imagedef 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 resultsdef 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:    passdef 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 imageiodef 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)  returndef 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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邵武市| 社旗县| 汶川县| 枣阳市| 翼城县| 铅山县| 呼图壁县| 沙洋县| 和平区| 洛南县| 龙江县| 泰宁县| 上饶市| 上饶县| 曲阳县| 高平市| 噶尔县| 林州市| 宕昌县| 莱芜市| 高碑店市| 边坝县| 鄂伦春自治旗| 博罗县| 林芝县| 南木林县| 武义县| 白玉县| 化隆| 西和县| 临湘市| 潮州市| 吴忠市| 永登县| 平定县| 平顶山市| 梧州市| 鹰潭市| 城市| 蓬莱市| 淮滨县|