Create an Animated Progress Bar GIF using Python

Handhika Yanuar Pratama
2 min readFeb 18, 2024

--

Photo by Cexin Ding on Unsplash

In this article, I want to share about how to create an animated progress bar GIF using Python and the Pillow library. The Pillow library is a powerful tool for image processing, and it includes functionality for creating, modifying, and saving images in various formats, including GIF.

Preparation

To begin, make sure you already have Pillow. We can install it using the following command.

pip install Pillow

Once Pillow is installed, we can use the provided Python script to generate a simple yet visually appealing progress bar GIF. The dimensions, frames, and output file path are customizable, allowing you to tailor the progress bar to your specific needs.

Source Code

Import the library

from PIL import Image, ImageDraw

Next, we can customize the width, height, frames, and output file path to meet your requirements.

width, height = 300, 50
frames = 100
output_gif_path = 'progress_bar.gif'

Next, we creates a list of frames by iterating through the specified number of frames. Each frame consists of a blank image with a drawn progress bar.

frames_list = []

for i in range(frames):
# Create a blank image
img = Image.new('RGB', (width, height), color='white')
draw = ImageDraw.Draw(img)

The progress bar is drawn with a dynamic width based on the current frame, creating the animation effect.

for i in range(frames):
...

# Draw the progress bar background
draw.rectangle([0, 0, width, height], outline='black', width=2)

# Calculate the progress bar width based on the current frame
progress_width = (i + 1) * (width - 4) // frames

# Draw the progress bar
draw.rectangle([2, 2, progress_width + 2, height - 2], outline='blue', fill='blue')

# Append the frame to the list
frames_list.append(img)

Finally, the script saves the frames as a GIF file, specifying the duration each frame is displayed and setting the loop parameter to 0 for infinite looping.

# Save the frames as a GIF
frames_list[0].save(output_gif_path, save_all=True, append_images=frames_list[1:], duration=50, loop=0)

The Output

After we generate the scripts, we can run the program and it should work perfectly like this.

Output of the Program

Conclusion

Creating animated progress bars or GIFs in Python can be both fun and practical. With the Pillow library, we have the flexibility to customize the appearance and behavior of your progress bar. Feel free to experiment with different colors, dimensions, and animation speeds to create visually engaging progress indicators for your projects.

--

--

Handhika Yanuar Pratama
Handhika Yanuar Pratama

Written by Handhika Yanuar Pratama

Live the Way the Life ask for || A Stoic Engineer || Technical Writer || Runner x Dreamer

No responses yet