# Let's create a GIF with python

It might that the word GIF (Graphics Interchange Format) you can't define, but what about the image below:

<center><a href="https://imgur.com/wjwU9dr"><img src="https://i.imgur.com/wjwU9dr.gif" title="source: imgur.com" /></a></center>

Yes, you’ve definitely seen one before.
<p> In this blog, we will create a GIF from scratch using only python.</p>
<br>

<h3>Overview</h3>

<ol>
<li> What is GIF
<li> Why we should create ours
<li> PIL library
<li> Show case
</ol>
<hr>

<h3>What is GIF</h3>
<p> GIF stands for Graphics Interchange Format, is a file type for images that has a fixed size, introduced by [CompuServe](https://www.compuserve.com/) 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.</p>
<p> Many websites enable to create GIFs for free such as [Giphy](https://giphy.com/create/gifmaker), [makeagif](https://makeagif.com/)...</p>
<h3>Why we should create ours</h3>
<p> 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](https://www.techrepublic.com/article/python-overtakes-java-to-become-the-second-most-popular-programming-language/) used as a programming language nowadays. And it has already a bunch of libraries to build projects in many fields, and [PIL](https://pillow.readthedocs.io/en/stable/) is one of them.
<h3>PIL library</h3>
<p> 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](https://github.com/python-pillow/Pillow)).</p>

- Installation
<p> Python provides the commands line to install [PIL](https://pillow.readthedocs.io/) under Windows, macOS, and Linux:</p>

```
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow

```
<p>Feel free to reach the PIL library website for more installation [details](https://pillow.readthedocs.io/en/stable/installation.html#warnings)</p>

<h3>Showcase</h3>
<p> In this section, we will see how to create a GIF together.  </p>

- Resize Images
<p> 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](https://giphy.com/), [makeagif](https://makeagif.com/) (like the GIF above).</p>
<p> Before converting our images to GIF, we must make surt we have the same size for all images.</p>

<p>In order to check images size, python has a function that returns the size of an image or for several images with PIL library:</p>

```
from PIL import Image

# first, we must open the image to read it.
img = Image.open("image.png")
# print the size
print(img.size)
```

<p>the output should return the width and the height of the image:</p>

```
(128, 128) #(width, height)
```

<p> 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:</p>

```
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)
```

<p>the output should like that:</p>

```
(128, 128)
(128, 128)
(128, 128)
(128, 128)
(128, 128)
```

<p>Now we can check the size of one or several images, let's see how we can resize them.</p>

```
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)
```
<p> The code above, should resize all the images in our path and we use print() to be sure about the new size:</p>

```
(100, 100)
(100, 100)
(100, 100)
(100, 100)
(100, 100)
```
- Create the GIF
<p>Either choosing customizable images or random ones, the goal of the showcase is to show how to get the right output.</p>
<p>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)
```
</p>

- Let's break the code:**
<p>
1- "img.save()" to save the GIF, include options to create the GIF.</p>
<p>
2- "format", to define the format of the file we want to create.</p>
<p>
3- "append_images=imgs", passing the images list.</p>
<p>
4- "save_all=True", If true, Pillow will save all frames of the image to a multiframe tiff document.</p>
<p>
5- "duration=300", timer to play all images.</p>
<p>
6- "loop=0", the GIF file will loop through its images n time.</p>

<p> The output should be like that:
<a href="https://imgur.com/8MST0E5"><img src="https://i.imgur.com/8MST0E5.gif" title="source: imgur.com" /></a></p>
<hr>
<p>This is it, hope you enjoy my blog and helpful enough to let you learn something new today.
