Friday, May 28, 2021

Create GIF Image with Python

GIF image

Create GIF image with Python

import numpy as np
import matplotlib.pyplot as plt
import imageio

# define the number of images
n=10

# create n-images of gaussian functions
x=np.linspace(0.0, 1.0, 100)
w=0.1
def gauss(t):
	return np.exp(-(x-t)**2/w**2)
T= np.linspace(0.0, 1.0, n)
for i, t in enumerate(T):
	f, ax = plt.subplots()
	ax.plot(x, gauss(t))
	plt.savefig(f"{i}-gauss.png")  

# create gif image from the n-images
images = []
filenames = [f"{i}-gauss.png" for i in range(n)]
for filename in filenames:
images.append(imageio.imread(filename))
imageio.mimsave('gauss.gif', images)

The obtained result is

Popular Posts