The pillow (fork of PIL, the Python Image Library) module presents a simple way to create a gif out of pre-existing image files, demonstrated below. More documentation can be found here. The intermediate image files which are generated by this Gist are not included directly, but can be generated by running the Python script.
Last active
December 13, 2020 00:53
-
-
Save jakelevi1996/21a95b1f09d26c9f0bf7583befd680a0 to your computer and use it in GitHub Desktop.
Making a gif from image files using PIL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import PIL | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| def make_gif( | |
| output_name, | |
| output_dir, | |
| input_path_list, | |
| duration=100, | |
| optimise=False, | |
| ): | |
| """ Make gif using pre-existing image files, and save to disk. The gif will | |
| loop indefinitely. | |
| Inputs: | |
| - output_name: filename for the output gif (not including .gif file | |
| extension) | |
| - output_dir: directory that the output gif will be saved to | |
| - input_path_list: list of file names of images, each of which will form a | |
| single frame of the gif, in the order that they're specifed in the list. | |
| The file names should include the file extension (EG .png), as well as | |
| the directory name (if not in the current directory) | |
| - duration: the duration each frame of the gif should last for, in | |
| milliseconds. Default is 100 seconds | |
| - optimise: if True, attempt to compress the palette by eliminating unused | |
| colors. Default is False | |
| """ | |
| first_frame = PIL.Image.open(input_path_list[0]) | |
| if not os.path.isdir(output_dir): | |
| os.makedirs(output_dir) | |
| output_filename = "%s.gif" % output_name | |
| output_path = os.path.join(output_dir, output_filename) | |
| first_frame.save( | |
| output_path, | |
| format="gif", | |
| save_all=True, | |
| append_images=[PIL.Image.open(f) for f in input_path_list[1:]], | |
| loop=0, | |
| duration=duration, | |
| optimise=optimise | |
| ) | |
| def simple_plot(x, y, x_label, y_label, plot_name, dir_name, alpha): | |
| # Create figure and plot | |
| plt.figure(figsize=[8, 6]) | |
| plt.plot(x, y, "bo", alpha=alpha) | |
| # Format, save and close the figure | |
| plt.title(plot_name) | |
| plt.xlabel(x_label) | |
| plt.ylabel(y_label) | |
| plt.tight_layout() | |
| plt.grid(True) | |
| if not os.path.isdir(dir_name): | |
| os.makedirs(dir_name) | |
| file_name = "%s.png" % plot_name | |
| full_path = os.path.join(dir_name, file_name) | |
| plt.savefig(full_path) | |
| plt.close() | |
| n_frames = 10 | |
| x = np.linspace(0, 1, 100) | |
| t = np.linspace(0, 1, n_frames, endpoint=False) | |
| dir_name = "." | |
| image_path_list = [] | |
| # First of all, save some image files to disk | |
| for t_i in t: | |
| y = np.sin(2 * np.pi * (x - t_i)) | |
| plot_name = "Test make gif, t = %.2f" % t_i | |
| simple_plot(x, y, "x", "y", plot_name, dir_name, 1) | |
| output_filename = "%s.png" % plot_name | |
| output_path = os.path.join(dir_name, output_filename) | |
| image_path_list.append(output_path) | |
| # Now turn image files into a gif | |
| make_gif("Test make gif", dir_name, image_path_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
