最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python圖像處理之gif動態(tài)圖的解析與合成操作詳解

 更新時間:2018年12月30日 15:01:55   作者:PHILOS_THU  
這篇文章主要介紹了Python圖像處理之gif動態(tài)圖的解析與合成操作,結(jié)合實例形式分析了Python基于PIL模塊解析gif文件,以及基于imageio庫合成gif文件的相關(guān)操作技巧,需要的朋友可以參考下

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

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

一、gif動態(tài)圖的合成

如下圖,是一個gif動態(tài)圖。

gif動態(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é)果如下,由此可見改動態(tài)圖實際上是由14張相同分辨率的靜態(tài)圖組合而成

二、gif動態(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幅圖,間副的間隔時間為0.1s,合成新的gif動態(tài)圖如下:

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)學(xué)運算技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

  • python三大器之迭代器、生成器、裝飾器

    python三大器之迭代器、生成器、裝飾器

    迭代是Python最強大的功能之一,是訪問集合元素的一種方式;迭代器是一個可以記住遍歷的位置的對象,本文給大家介紹python三大器之迭代器、生成器、裝飾器的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧
    2022-01-01
  • 使用 Python 清理收藏夾里已失效的網(wǎng)站

    使用 Python 清理收藏夾里已失效的網(wǎng)站

    這篇文章主要介紹了用 Python 清理收藏夾里已失效的網(wǎng)站,本文通過截圖實例代碼的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • 一文了解python 3 字符串格式化 F-string 用法

    一文了解python 3 字符串格式化 F-string 用法

    本文介紹在python 3 編程中,如何進行字符串格式化。介紹了F-string的用法,通過實例代碼給大家介紹的非常詳細,對大家的工作或?qū)W習(xí)具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-03-03
  • win10安裝python3.6的常見問題

    win10安裝python3.6的常見問題

    在本篇文章里小編給大家分享的是關(guān)于win10安裝python3.6的具體步驟,有興趣的朋友們可以參考學(xué)習(xí)下。
    2020-07-07
  • 詳解Python中for循環(huán)的使用方法

    詳解Python中for循環(huán)的使用方法

    這篇文章主要介紹了Python中for循環(huán)的使用方法,是Python入門中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05
  • Django migrate報錯的解決方案

    Django migrate報錯的解決方案

    在講解如何解決migrate報錯原因前,我們先要了解migrate做了什么事情,本文就詳細的介紹migrate使用以及出現(xiàn)問題的解決,感興趣的可以了解一下
    2021-05-05
  • python解壓zip包中文亂碼解決方法

    python解壓zip包中文亂碼解決方法

    這篇文章主要介紹了python解壓zip包中文亂碼解決方法,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-11-11
  • 利用Python-iGraph如何繪制貼吧/微博的好友關(guān)系圖詳解

    利用Python-iGraph如何繪制貼吧/微博的好友關(guān)系圖詳解

    這篇文章主要給大家介紹了關(guān)于利用Python-iGraph如何繪制貼吧/微博好友關(guān)系圖的相關(guān)資料,文中顯示介紹了在windows系統(tǒng)下安裝python-igraph的步驟,然后通過示例代碼演示了繪制好友關(guān)系圖的方法,需要的朋友可以參考下。
    2017-11-11
  • python?ES連接服務(wù)器的方法詳解

    python?ES連接服務(wù)器的方法詳解

    使用Python連接Elasticsearch服務(wù)器進行數(shù)據(jù)搜索和分析是一項常見操作,本文詳細介紹了如何使用elasticsearch-py客戶端庫連接到Elasticsearch服務(wù)器,并執(zhí)行創(chuàng)建索引、添加文檔及搜索等基本操作
    2024-10-10
  • Python+folium繪制精美地圖的示例詳解

    Python+folium繪制精美地圖的示例詳解

    folium是一個基于leaflet.js的python地圖庫,可以通過folium來操縱數(shù)據(jù),并將其可視化。本文將通過各種示例詳細講解如何利用folium繪制精美地圖,需要的可以參考一下
    2022-03-03

最新評論

甘南县| 太和县| 炉霍县| 晋城| 高青县| 岢岚县| 老河口市| 沧源| 射洪县| 于都县| 湘潭市| 惠来县| 云南省| 鄂伦春自治旗| 久治县| 准格尔旗| 肥西县| 仙游县| 荣成市| 上饶县| 宿松县| 寿阳县| 石林| 雷山县| 通江县| 冷水江市| 马山县| 镇赉县| 军事| 长顺县| 平定县| 西和县| 河池市| 都昌县| 三都| 喜德县| 赣州市| 岑溪市| 岗巴县| 宁德市| 灵寿县|