It might that the word GIF (Graphics Interchange Format) you can't define, but what about the image below:
Yes, you’ve definitely seen one before.
In this blog, we will create a GIF from scratch using only python.
Overview
- What is GIF
- Why we should create ours
- PIL library
- Show case
What is GIF
GIF stands for Graphics Interchange Format, is a file type for images that has a fixed size, introduced by CompuServe in 1987. Nowadays it's widely used in social media posts and comments. GIF comes with a more simple way to share, add more context or emotion in a much shorter message.
Many websites enable to create GIFs for free such as Giphy, makeagif...
Why we should create ours
As a developer, create projects is very necessary, even small ones, in order to improve our coding and logic thinking skills. Also Python is widely used as a programming language nowadays. And it has already a bunch of libraries to build projects in many fields, and PIL is one of them.
PIL library
PIL (PILLOW) stands for "Python Imaging Library. provides extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities (learn more here).
- Installation
Python provides the commands line to install PIL under Windows, macOS, and Linux:
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow
Feel free to reach the PIL library website for more installation details
Showcase
In this section, we will see how to create a GIF together.
- Resize Images
The first thing we must know is that with the PIL library that it supports only images to create GIF, not videos. To create a GIF from a video, you need a video editor in order to customize the GIF scene that you want, you can render to Giphy, makeagif (like the GIF above).
Before converting our images to GIF, we must make surt we have the same size for all images.
In order to check images size, python has a function that returns the size of an image or for several images with PIL library:
from PIL import Image
# first, we must open the image to read it.
img = Image.open("image.png")
# print the size
print(img.size)
the output should return the width and the height of the image:
(128, 128) #(width, height)
To run several images in GIF format, we must have the same size (width, height) for all images, to do that we can refer to the code below:
from PIL import Image
import glob
# we store the full path of the images in the variable "path".
path = "/home/moez/genqrcode/photos/*.png"
# we loop through images with the extension ".png" using glob module.
for i in glob.glob(path):
img = Image.open(i)
print(img.size)
the output should like that:
(128, 128)
(128, 128)
(128, 128)
(128, 128)
(128, 128)
Now we can check the size of one or several images, let's see how we can resize them.
import PIL
import os
import os.path
from PIL import Image
f = r'/resources/images'
for file in os.listdir(f): # use the directory content with listdir()
f_img = f+"/"+file
img = Image.open(f_img)
img = img.resize((100, 100)) # (width, height)
img.save(f_img) # save images with the new size
print(f_img)
The code above, should resize all the images in our path and we use print() to be sure about the new size:
(100, 100)
(100, 100)
(100, 100)
(100, 100)
(100, 100)
- Create the GIF
Either choosing customizable images or random ones, the goal of the showcase is to show how to get the right output.
The code below shows the steps on how to:
import glob
from PIL import Image
# filepaths
fp_in = "/home/moez/genqrcode/photos/*.png"
fp_out = "/home/moez/genqrcode/img.gif"
# loop through all images
img, *imgs = [Image.open(f) for f in sorted(glob.glob(fp_in))]
# create the GIF image
img.save(fp=fp_out, format='GIF', append_images=imgs,
save_all=True, duration=500, loop=0)
- Let's break the code:**
1- "img.save()" to save the GIF, include options to create the GIF.
2- "format", to define the format of the file we want to create.
3- "append_images=imgs", passing the images list.
4- "save_all=True", If true, Pillow will save all frames of the image to a multiframe tiff document.
5- "duration=300", timer to play all images.
6- "loop=0", the GIF file will loop through its images n time.
The output should be like that:
This is it, hope you enjoy my blog and helpful enough to let you learn something new today.