Images constitute a substantial part of our lives, whether on the internet, in books, journals, or magazines. They help in grasping the relevant information quicker and more effectively convey a specific message to the observers. Working with these images is also a significant part of programming, especially in the fields of computer vision and image processing.
Looking at individual images is fun, but pairing them together to create a small animated file to add further meaning and value to the image allowing the observers to gain additional information, is intriguing. While videos or movies are a constitution of a bunch of pictures paired together, they are often lengthy.
However, the Graphics Interchange Format (GIF) developed in 1987 supports up to 8 bits per pixel and can contain 256 indexed colors. They can be used to display still images or a small animated file, usually lasting a few seconds. These GIF files are like flipbooks that help to communicate unique ideas, humor, and emotion or convey meaningful expressions.
In this article, we will work on the GIF creation project with the help of images that, when paired together in a sequence, help to contribute to a specific meaning or action. We will utilize two fundamental Python programming libraries and develop our project with some simple code. I will also cover some additional improvements to enhance the functionality of the project that the readers can try out.
Before getting started with this project, I would recommend checking out one of my previous articles, where I cover seven essential programming tips that one can follow to improve Productivity with the Python programming language. The link to the following article is provided below, covering a brief guide on how to fix some of the common inefficient programming practices.
Developing the GIF creator project with Python:
In this section, we will focus on developing a GIF file by utilizing a set of images that depict a particular action. By compiling a list of similar images, or certain images that illustrate a specific activity, we can compute the GIF files accordingly.
I am using the following three free images – image 1 (Sunrise), image 2 (Sunset), and image 3 (night) from Unsplash for this gif file. The readers can feel free to implement their own images for their respective GIF files.
Once the developers have selected their desired images, we can proceed to start the project by importing the essential libraries that we will utilize for the development of this project.
Importing the essential libraries:
The two essential libraries that we will use for this project are the Pillow (PIL) library and the glob module. We utilize these libraries for working with images and the directories in which the images are stored. The pillow library allows us to load the respective images and perform operations on them. These operations include resizing and saving the information accordingly. The glob module allows us to directly access all the images of a particular format within a particular directory allowing for faster computation of this project.
# Importing the essential libraries
from PIL import Image
from glob import glob
Another alternative library that we can utilize is in the in-built os Python module that will allow us to perform operations on the operating system. Using this library, we can directly open the GIF file through our program once it is created.
Initializing the required parameters:
The primary parameters that we will create for the purpose of this GIF creator project are the image lists containing the respective image for each frame and another parameter for storing the list of images from the specified directory. The other variables that we will consider are the width and height of the image dimensions as well as the name for storing the created GIF file. The code block for the respective parameters and variables is mentioned below.
# Mention the Resizing Dimensions and save file name
w = 1024
h = 1024
image_name = "Day_Cycle_1024.gif"
# Creating a list of images
image_list = []
_images = glob("images/*.jpg")
Once we have defined the fundamental variables and parameters required for this code block, we can proceed to develop the looping cycle for creating the desired GIF file with the stored images.
Developing the looping cycle:
In the next step, we will loop through the images directory containing the stored images (in .jpg format) and retrieve each individual image. Using the pillow library, we will open each image stored in the directory and resize them to the desired width and height. Note that the resizing step is critical to maintaining the aspect ratio of the entire GIF file uniformly. Once each image is resized, we can store them in a list file through which the GIF file can be created. The code block for the following steps is mentioned below.
# Creating the for loop with the respective images
for _image in _images:
# Opening each individual image
file = Image.open(_image)
# Resizing the images to a single matching dimension
file = file.resize((w, h))
# Creating a list of all the images
image_list.append(file)
Creating the GIF file:
The final step to creating the GIF file is to store the desired images created in the list in a GIF file format. We can make use of the save command in the pillow library to create the required GIF file. The primary attributes required to create the following are the image name (which we previously defined), the format for saving the file, the list of images to be appended to the original image, the save state, the duration, and the loop. We can finally print that the GIF file is created once the Python code runs successfully. The code block for creating the GIF file is mentioned below.
# Create the GIF File
image_list[0].save(image_name, format='GIF',
append_images=image_list[1:],
save_all=True,
duration=500, loop=0)
print("The GIF file is successfully created!")
Once the save file is created, you can access it directly from the respective working directory of the Python code.
Complete code and additional tips:
If the readers have followed all the steps until this point accurately, you can now access the created GIF file. Once the GIF file is created and accessed from the working directory, the GIF image displayed above can be viewed in the Python folder, where the code was run.
The complete code for the project is provided below to the readers for easier access.
# Importing the essential libraries
from PIL import Image
from glob import glob
# Mention the Resizing Dimensions and save file name
w = 1024
h = 1024
image_name = "Day_Cycle_1024.gif"
# Creating a list of images
image_list = []
_images = glob("images/*.jpg")
# Creating the for loop with the respective images
for _image in _images:
# Opening each individual image
file = Image.open(_image)
# Resizing the images to a single matching dimension
file = file.resize((w, h))
# Creating a list of all the images
image_list.append(file)
# Create the GIF File
image_list[0].save(image_name, format='GIF',
append_images=image_list[1:],
save_all=True,
duration=500, loop=0)
print("The GIF file is successfully created!")
The additional quality of life improvements that I would recommend the viewers to check out and try by themselves to further improve the project would be as follows:
- Create a list/loop for the various image formats such as .png, .jpeg, and .tif files so that all the images, regardless of the format, can be included in the GIF file that is being created. In the current project, only the .jpg files are considered from the respective image folder.
- Make use of the os library module or other similar libraries to directly display the GIF file once the program is executed. Doing this step will allow the developers to skip having to access the created GIF file from the working directory each time the program is run.
Conclusion:
"When to use iterative development? You should use iterative development only on projects that you want to succeed." – Martin Fowler
Images are a critical part of life. There is so much to adore in this beautiful world, and images are one of the most wonderful ways to perceive the world we live in. Moving one step forward from images are GIF files, which are constituted of a number of images compiled together to create meaningful or fun content.
In this article, we explored a straightforward Python project that we can easily develop with the help of two fundamental libraries, Pillow and glob. We utilized some basic variables and parameters to create an elementary looping code block through which we could iterate the images to create the required GIF file. Further additional improvements can be made to the suggested to enhance its usefulness.
On a side note, I am sorry for the lack of continuous content for my viewers, as I have been extremely busy for the past two months. However, I will be a lot freer in the upcoming months and will be able to provide more content. So stay tuned for more cool projects!
If you want to get notified about my articles as soon as they go up, check out the following link to subscribe for email recommendations. If you wish to support other authors and me, then subscribe to the below link.
If you have any queries related to the various points stated in this article, then feel free to let me know in the comments below. I will try to get back to you with a response as soon as possible.
Check out some of my other articles in relation to the topic covered in this piece that you might also enjoy reading!
The Ultimate Replacements to Jupyter Notebooks
7 Best Research Papers To Read To Get Started With Deep Learning Projects
Thank you all for sticking on till the end. I hope all of you enjoyed reading the article. Wish you all a wonderful day!