Home  >  Article  >  Backend Development  >  How to make gif in python

How to make gif in python

(*-*)浩
(*-*)浩Original
2019-06-22 13:36:222808browse

Recently, I accidentally saw a friend’s public account mentioning the use of Python to generate GIF animations. It looked interesting, so I gave it a try. In fact, there are a lot of software for making animations, but it is quite good to realize it by using code when you have enough food and clothing.

How to make gif in python

This production process is very simple. Let’s paste the code directly. Take the code as an example: (Recommended learning: Python video tutorial)

First of all! Install the imageio library; follow the comments in the following code, first read the static image into the list as each frame of GIF; then set the input (static image), output (dynamic image) and some necessary parameters, we set each frame here The interval is 1.5 seconds, the default is 1 second, and then the miageio.mimsave function is called to save the result.

import imageiodef create_gif(image_list, gif_name, duration = 1.0):
    '''
    :param image_list: 这个列表用于存放生成动图的图片
    :param gif_name: 字符串,所生成gif文件名,带.gif后缀
    :param duration: 图像间隔时间
    :return:
    '''
    frames = []    for image_name in image_list:
        frames.append(imageio.imread(image_name))

    imageio.mimsave(gif_name, frames, 'GIF', duration=duration)    returndef main():
    #这里放上自己所需要合成的图片
    image_list = ['1.jpg', '2.jpg', '3.jpg']
    gif_name = 'new.gif'
    duration = 1.5
    create_gif(image_list, gif_name, duration)if __name__ == '__main__':
    main()

In python, you can create the above animation with just a few lines of code. In addition, the pictures made through this method seem to have no restrictions on static pictures. Very useful.

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to make gif in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn