Make lossless GIF animations with python

Let’s talk about the goal of organizing

  1. Select favorite photos to make GIF images with python

  2. The animation is too big, compress it losslessly

Generate gif

It can be easily implemented by calling the python library imageio

  • Read the static image into the list as each frame of the GIF animation

  • Set the duration of each frame interval, I set it to 1.15 seconds

  • Just call the function imageio.mimsave of the imageio library

Technology Exchange

Technology must learn to share and communicate, and it is not recommended to work behind closed doors. A person can go fast, a group of people can go farther.

The source code, information, data, and technical exchange improvements in the article can be obtained by adding the knowledge planet exchange group. The group has more than 2,000 members. When adding, remember to add the following remarks: source + interest direction, which is convenient for finding like-minded friends.

Method ①, add WeChat account: pythoner666, remarks: from CSDN + code
Method ②, WeChat search official account: Python learning and data mining, background reply: add group

import imageio

def create_gif(image_list, gif_name, duration=0.35):
    frames = []
    for image_name in image_list:
        frames.append(imageio.imread(image_name))
    imageio.mimsave(gif_name, frames, 'GIF', duration=duration)
    return

def main():
    image_list = ['car/10.jpg','car/15.jpg','car/8.jpg',]
    gif_name = 'car/car.gif'
    duration = 1.15
    create_gif(image_list, gif_name, duration)

if __name__ == '__main__':
    main()

Results as shown below

By the way

I originally used the software ScreenToGif to make GIFs. The animations it records are really big, and the editing is a bit stuck. I don’t need it.

lossless compression

The previous GIF animation is compressed, only more than 3M, and the original size is close to 50MB.

If it is not compressed, friends need to waste 50M traffic download.

Resources are precious, first use the favorite web pages to compress and try

There are still more than 20 M after compression, the quality is lost, and there is no compression ratio parameter to choose from.

It’s better to use python, there are free modules, so you don’t need to find these resources to register online

Use ImageIo and PIL libraries to compress gif animations and import them first

import imageio
from PIL import Image, ImageSequence

Set the compression size, here set the compression size to 1000

rp = 1000   

After 50M compression, the size will be reduced to 4M. If it is set to 500, the compression will be less than 1M

The next code is as follows

img_list = []

# 读取原gif动图
img = Image.open("car/car.gif")

# 对原动图进行压缩,并存入img_list 
for i in ImageSequence.Iterator(img):
    i = i.convert('RGB')
    if max(i.size[0], i.size[1]) > rp:
        i.thumbnail((rp, rp))
    img_list.append(i)

# 计算帧的频率
durt = (img.info)['duration'] / 1000

# 读取img_list合成新的gif
imageio.mimsave('car/car1.gif', img_list, duration=durt )

After the compression is completed, it is 3.51MB, which is not a little bit smaller, and it is displayed losslessly

Guess you like

Origin blog.csdn.net/m0_59596937/article/details/130095247