Write a gif generator in Python by yourself, Pillow is really easy!

created at 08-04-2021 views: 3

I wanted to make a gif picture a few days ago. I searched the Internet for a while but didn’t find a suitable gif production software. Finally, I asked the students in the group to recommend a gif software called gifCam. It was good to use, only a few The size of a hundred K, I think I can also make a gif maker, so today is mainly for this matter.

Not much nonsense, I went directly to the technical stage. I found a graphics library called Pillow through searching in python. After researching it, I found that it can solve my own problem.

1. Demand analysis

My requirement is to be able to record gif pictures, because it is the first time to do this kind of thing, so I need to break down the requirements.

Step 1: I need to be able to record the screen, that is, the screen capture function, save the current screen, the technical plan is temporarily undecided, postponed!

Step 2: I need to be able to merge the screenshots and synthesize gif images. I have found a suitable library and do it immediately.

Step 3: Make the python file a friendly interface, non-core functions, postpone.

gif generated with pillow

The core function of the whole is to synthesize pictures, so what I have to do today is to find some pictures to synthesize into gif

2. Technical analysis

The environment on my machine is python3.8

The required package is Pillow. Note that it needs to match your python version. It is recommended to use the following method to install

3. Environmental installation

Enter the following command at the console:

pip install Pillow

4. Source code

# coding:utf-8
import os
from PIL import Image

imgFolderPath = "E:\\child\\bug\\WxBot\\xin\\beauty"
fileList = os.listdir(imgFolderPath)
firstImgPath = os.path.join(imgFolderPath, fileList[0])
im = Image.open(firstImgPath)
images = []
for img in fileList[1:]:
   imgPath = os.path.join(imgFolderPath, img)
im.save('beauty.gif', save_all=True,append_images =images,loop = 0,duration=2000)

Code interpretation:

  • imgFolderPath: is the folder where the picture is located
  • os.listdir(imgFolderPath) lists all file names and returns the list
  • firstImgPath = os.path.join(imgFolderPath, fileList[0]) The absolute address of the first image to be spliced
  • im = Image.open(firstImgPath) takes the first file as the first frame
  • for img in fileList[1:]: traverse all the remaining pictures, read and add to the images list
  • im.save('beauty.gif', save_all=True,append_images =images,duration=2000) The core function of the current function
  • beauty.gif is the full name of the gif file to be saved
  • save_all save all images
  • duration=2000 Play each frame of picture for 2000 milliseconds
  • loop = 0 GIF pictures keep looping repeatedly (loop=0, if set to 1, then loop once, set to 2, loop twice, and so on)
  • Execute the above code, you can see that beauty.gif is generated in the directory where py is located, you can use the picture-viewing software to see it, perfect.
created at:08-04-2021
edited at: 08-04-2021: