Skip to content

Commit

Permalink
Update create-a-gif-with-python.mdx
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnynomnom authored Nov 16, 2023
1 parent 5af530b commit 356b0d4
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions projects/create-a-gif-with-python/create-a-gif-with-python.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ tags:
<BannerImage link="https://raw.githubusercontent.com/codedex-io/projects/main/projects/create-a-gif-with-python/header.gif" description="Header GIF" uid={true}/>

**Prerequisite:** Python fundamentals
**Versions:** Python 3.7+, imageio 2.16.2
**Versions:** Python 3.10, imageio 2.16.2
**Read Time:** 30 minutes

## Introduction

Do you pronounce it “GIF” or a “JIF”? Either way, [Graphics Interchange Format (GIF)](https://en.wikipedia.org/wiki/GIF) is great for creating animated images. The format has been around since 1987 and helped define the early internet. It’s used to display memes, graphics, logos, and they are everywhere — on websites, blogs, texts, and social media.
Do you pronounce it “GIF” or a “JIF”? Either way, [Graphics Interchange Format (GIF)](https://en.wikipedia.org/wiki/GIF) is great for creating animated images. The format has been around since 1987 and helped define the early internet. It’s used to display memes, graphics, logos, and they are everywhere — on websites, blogs, text messages, and social media.

GIFs are “animated images” because they aren’t exactly videos. They are more like flipbooks; they don’t have sound and flip through multiple pictures sequentially.
GIFs are “animated images” because they aren’t exactly videos. They are more like flipbooks; they don’t have sound and flip through a bunch of pictures sequentially.

In this project tutorial, I will show you how to combine multiple images and create a GIF using just 6 lines of Python code! More specifically, a list, a `for` loop, and a Python library called `imageio`.
In this project tutorial, I will show you how to combine multiple images and create a GIF using just 6 lines of Python code! More specifically, a list, a `for` loop, and a library called `imageio`.

Here’s a preview of the project:

Expand All @@ -39,7 +39,7 @@ Here’s a preview of the project:

## ImageIO Library

[Imageio](https://imageio.readthedocs.io/en/stable/) is a Python library that provides an easy interface to read and write a wide range of image data. It runs on Python 3.5+.
[Imageio](https://imageio.readthedocs.io/en/stable/) is a Python library that provides an easy interface to read and write a wide range of image data. It runs on Python 3.5 and above.

Suppose you have Python and [pip](https://pip.pypa.io/en/stable) the package installer on your computer. In that case, you can install `imageio` using this command in the **Terminal** (Mac) or **Command Prompt** (Windows):

Expand All @@ -57,22 +57,24 @@ You can quickly check if the `imageio` package was successfully installed by ope

## Writing the Program

Let’s open up a code editor like [VS Code](https://code.visualstudio.com).
Let’s open up a code editor like [VS Code](https://code.visualstudio.com) and create a new file called **create_a_gif.py**.

To use the `imageio` library, you need to include it in your code. The "v3" in the import statement means you're using version 3 of the `imageio` library:
To use the `imageio` library, you need to import it in your code. The "v3" in the `import` statement means you're using version 3 of the `imageio` library:

```py
import imageio.v3 as iio
```

The `as iio` part allows you to give the library a shorter name to work with, making it more convenient. Now, run the code to make sure it works!
The `as` part allows you to give the library a shorter name to work with (a nickname/alias), making it more convenient. So we've renamed `imageio.v3` as `iio` moving forward.

Now, run the code to make sure it works. Hopefully there's no error!

Here are two images that you can use for this project (feel free to use your own!):

- [team-pic1.png](https://raw.githubusercontent.com/codedex-io/projects/main/projects/create-a-gif-with-python/team-pic1.png)
- [team-pic2.png](https://raw.githubusercontent.com/codedex-io/projects/main/projects/create-a-gif-with-python/team-pic2.png)

Make sure to store the image files in the same folder as the one that contains your Python program file.
💡 Make sure to store the image files in the same folder as your Python program file.

In our Python program, we'll create a list that contains the locations of the image files. We also need to create an empty list that will be used to store the actual image data from these files.

Expand Down Expand Up @@ -117,17 +119,18 @@ for filename in filenames:
iio.imwrite('team.gif', images, duration = 500, loop = 0)
```

Let’s run this program and see what happens! A new `team.gif` appeared:
Let’s run this program and see what happens! 🪄 A new **team.gif** should appear:

<RoundedImage link="https://raw.githubusercontent.com/codedex-io/projects/main/projects/create-a-gif-with-python/team.gif" description="team.gif" />

## Wrapping Up

Congrats! You created a GIF in just 6 lines of code. Now you don’t have to rely on paid online tools to sew your images into GIFs. You just need a list, a `for` loop, and the `imageio` library to do the trick.
Congrats! You created a GIF in just 6 lines of code. Now you don’t have to rely on paid online tools to sew your images into GIFs. You just need a list, a `for` loop, and the `imageio` library to do the trick.

Let me know what GIFs you have created by tagging [@codedex_io](https://twitter.com/codedex_io) on Twitter!

### More Resources

- [Solution on GitHub](https://github.com/codedex-io/projects/blob/main/projects/create-a-gif-with-python/create_a_gif.py)
- [Set Up Your Local Development Environment with Python](https://www.codedex.io/projects/set-up-your-local-environment-in-python)
- [Imageio](https://imageio.readthedocs.io/en/stable/index.html)

0 comments on commit 356b0d4

Please sign in to comment.