欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

本文實(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的exec、eval使用分析

    python的exec、eval使用分析

    這篇文章主要介紹了python的exec、eval使用分析,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Python實(shí)現(xiàn)登錄人人網(wǎng)并抓取新鮮事的方法

    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)最小二乘法與梯度下降算法

    這篇文章主要介紹了利用Python實(shí)現(xiàn)最小二乘法與梯度下降算法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • python字符串連接的N種方式總結(jié)

    python字符串連接的N種方式總結(jié)

    python中有很多字符串連接方式,今天在寫代碼,順便總結(jié)一下,從最原始的字符串連接方式到字符串列表連接,大家感受下
    2014-09-09
  • QML實(shí)現(xiàn)鐘表效果

    QML實(shí)現(xiàn)鐘表效果

    這篇文章主要為大家詳細(xì)介紹了QML實(shí)現(xiàn)鐘表效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • Python中的HTTP請求庫Requests的具體使用

    Python中的HTTP請求庫Requests的具體使用

    Python作為一種功能強(qiáng)大且易于學(xué)習(xí)的編程語言,提供了許多用于處理HTTP請求的庫,其中,Requests庫是最受歡迎的選擇之一,本文主要介紹了Python中的HTTP請求庫Requests的具體使用,感興趣的可以了解一下
    2023-12-12
  • python中利用Future對象異步返回結(jié)果示例代碼

    python中利用Future對象異步返回結(jié)果示例代碼

    future是一種對象,表示異步執(zhí)行的操作。下面這篇文章主要給大家介紹了關(guān)于python中利用Future對象異步返回結(jié)果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-09-09
  • 在pyCharm中下載第三方庫的方法

    在pyCharm中下載第三方庫的方法

    這篇文章主要介紹了在pyCharm中下載第三方庫的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • python正則表達(dá)式的懶惰匹配和貪婪匹配說明

    python正則表達(dá)式的懶惰匹配和貪婪匹配說明

    這篇文章主要介紹了python正則表達(dá)式的懶惰匹配和貪婪匹配說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Python MD5文件生成碼

    Python MD5文件生成碼

    用python實(shí)現(xiàn)文件md5生成碼核心實(shí)現(xiàn)代碼。
    2009-01-01

最新評論