Home  >  Article  >  Backend Development  >  Introduction to Python's analysis and synthesis of gif dynamic graphics

Introduction to Python's analysis and synthesis of gif dynamic graphics

不言
不言forward
2018-12-31 09:24:274068browse

This article brings you an introduction to the analysis and synthesis operations of Python's processing of gif dynamic images. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The example in this article describes the analysis and synthesis operations of GIF dynamic graphics in Python image processing. I would like to share it with you for your reference. The details are as follows:

Gif animated pictures are now commonplace, and people often fight over them in the circle of friends when they disagree. Here, I will introduce how to use python to parse and generate gif images.

1. Synthesis of gif dynamic graph

The following picture is a gif dynamic graph.

PILThe image module can be used to parse gif dynamic images. The specific code is as follows:

#-*- 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()

The analysis results are as follows, by This visible dynamic image is actually a combination of 14 static images of the same resolution

2. Synthesis of gif dynamic images

Gif image synthesis, using the imageio library (https://pypi.python.org/pypi/imageio)

The code is as follows:

#-*- 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()

Here, using the 8 images parsed in the first step, the interval between images is 0.1s, and the new gif dynamic image is synthesized as follows:

The above is the detailed content of Introduction to Python's analysis and synthesis of gif dynamic graphics. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete