您的当前位置:首页正文

如何使用Python快速生成gif图

2024-11-08 来源:个人技术集锦

前言

imageio介绍

imageio安装

pip install imageio

合成gif图

import imageio
def compose_gif():
    img_paths = ["img/1.jpg","img/2.jpg","img/3.jpg","img/4.jpg"
    ,"img/5.jpg","img/6.jpg"]
    gif_images = []
    for path in img_paths:
        gif_images.append(imageio.imread(path))
    imageio.mimsave("test.gif",gif_images,fps=1)

读取特殊url图片

import imageio
import matplotlib.pyplot as plt

def read_special_url_image():
    img = imageio.imread("imageio:chelsea.png")
    plt.imshow(img)
    plt.show()
    url = 'http://upload.wikimedia.org/wikipedia/
    commons/d/de/Wikipedia_Logo_1.0.png'
    img = imageio.imread(url)
    url = "https://timgsa.baidu.com/timg?image&quality=80&size=
    b9999_10000&sec=1545324012100&di=77a4ba3d60cdfc60
    d161bd6ed97c2319&imgtype=0&src=http%3A%2F%2Fwww.hxzyt
    j.com%2Fthemes%2Ftheme_3%2Fpc%2Fimages%2Fpython%2Fimg_01.jpg"
    img = imageio.imread(imageio.core.urlopen(url).read(), '.jpg')
    #截屏
    img = imageio.imread("<screen>")
    #读取粘贴板的图片
    img = imageio.imread("<clipboard>")

读取视频

def get_video_per_frame():
    reader = imageio.get_reader("test.mp4")
    for i,im in enumerate(reader):
        print("Mean of frame %i is %1.1f"%(i,im.mean()))

在使用imageio读取视频的时候可能会报上面的错误,提示需要安装ffmpeg,可以根据提示进行安装即可.除此之外,imageio还提供了许多方便的接口,有兴趣的可以去了解一下.

总结

Top