1. Overview

WebP is a file format for images and animations released in 2010 by Google, as an all-in-one replacement for JPEG, PNG, and GIF. Strengths of WebP include efficiency in file size and support for lossy and lossless compression. However, generally, WebP isn’t supported by as many programs in comparison to traditional image formats.

In this tutorial, we cover two different ways to convert an animation from WebP to GIF. The first method uses ImageMagick and is the simplest, but may result in a larger file size. The second method uses Python and is more complex, but results in a smaller file. Finally, we create a Bash script, that automatically detects and converts animated WebP files to GIFs.

2. Converting WebP to GIF With ImageMagick

ImageMagick is a collection of software that enables the manipulation, conversion, and alteration of images. In this section, we go over installing ImageMagick. After that, we use ImageMagick to convert animated files from WebP to GIF format.

2.1. Installing

To begin with, we install ImageMagick. For Ubuntu, ImageMagick is provided in the default repository, so we install it using apt:

$ sudo apt install imagemagick

We install ImageMagick using apt install. Notably, the current version provided in the Ubuntu repository is ImageMagick version 6, with the latest version being 7.

2.2. Using ImageMagick

For ImageMagick version 6, we use the convert command to convert our file from WebP to GIF. However, for version 7, convert is deprecated, and instead, we should use the magick command.

Now, let’s convert a sample file:

$ convert baeldung_animation.webp baeldung_animation.gif

We just run convert with the input file as the first argument and the output file as the second argument.

Finally, let’s compare the output file size to the input file size:

$ du -h *.{webp,gif}
1.1M baeldung_animation.webp
1.7M baeldung_animation.gif

In this case, du shows the sizes of our WebP and GIF files with the -h flag formatting sizes using human-readable notation. As we can see, the resulting file is around 50% larger than the input file.

Luckily, a more efficient solution is possible but requires a different method.

3. Converting WebP to GIF With Python

Python is a high-level programming language. Additionally, Python has a large collection of third-party packages for extended functionality. In particular, Pillow is a Python module that enables simple image manipulation and conversion.

In this section, we learn how to install Pillow and then use it to convert animated WebP files to GIF.

3.1. Installing

Before using Pillow, we install it on the system via apt:

$ sudo apt install python3-pil

In Ubuntu, the Pillow package is under the name python3-pil, and may already be installed.

Further, for Python environments that are externally managed, we can also use pip.

3.2. Using Pillow

Once we have Pillow installed, we can begin converting WebP animations to GIF.

To start, let’s open up the Python shell and import Pillow:

$ python3
>>> from PIL import Image

We run python3 to start the Python shell. Next, we import Image from Pillow (PIL), importing the Image module in the current shell session.

Finally, we can convert a WebP file:

>>> new_image = Image.open("baeldung_animation.webp")
>>> new_image.save("baeldung_animation.gif", "gif", save_all=True, optimize=True)

The first thing we do is open the WebP file with Image.open, assigning it to the new_image variable. Following that, we run new_image.save passing the arguments:

  • baeldung_animation.gif: the file to save to
  • gif: the file format to use
  • save_all=True: save all frames in the animation
  • optimize**=True: optimize the output file

Finally, let’s take a look at the file size of the resulting converted file:

$ du -h *.{webp,gif}
1.1M    baeldung_animation.webp
788K    baeldung_animation.gif

As we can see, this method of conversion is a lot more efficient, with an actual reduction in size between the WebP animation and GIF.

4. Automatically Converting Animated WebP to GIF

Now that we know how to convert animated WebP files to GIFs, let’s create a script to automatically find and convert animated WebP files to GIFs.

4.1. Creating the Bash Script File

Firstly, we create a file for the script, making sure it’s executable:

$ touch convert_webp.sh
$ chmod +x convert_webp.sh

We create the script file using touch. Following that, we use the chmod command with +x to make it executable.

4.2. Writing the Bash Script

Next, let’s populate the Bash script:

$ cat convert_webp.sh
#!/bin/bash
# convert_webp.sh

find . -type f -iname '*.webp' -exec python -c """
from PIL import Image
file_name='{}'
my_image = Image.open(file_name)
if my_image.is_animated:
    print(f'Converting {file_name} to GIF')
    my_image.save(file_name.replace('.webp', '.gif'), 'gif', save_all=True, optimize=True)
""" \;

In this example, we use find to locate all the WebP files. Then, we employ python to check whether the WebP file is animated, and if so, we convert it to GIF.

Let’s break down the find command a little further:

  • -type f: search only for files
  • -iname ‘*.webp’: search for file names ending in .webp
  • -exec: execute an external command on each file

Now, let’s go over the Python code:

  • -c: pass program as a string
  • from PIL import IMAGE: import Image module from Pillow
  • fname='{}’: set fname to {} to be substituted by find -exec
  • Image.open(file_name): open the WebP file
  • if my_image.is_animated: check if the file is animated
  • my_image.save(file_name.replace(‘.webp’, ‘.gif’), ‘gif’, save_all=True, optimize=True): convert WebP file to GIF

Thus, we combine Python and Bash commands to perform batch conversion.

4.3. Running the Bash Script

Finally, we can execute the script:

$ . convert_webp.sh
Converting ./baeldung_animation.webp to GIF
...

In this example, we execute the script with . and the location of the script file. Additionally, we can make the script callable through just its filename by adding it to the PATH variable.

Afterward, all animated WebP files under the current directory are converted to GIFs.

5. Conclusion

In this article, we learned about two different methods of converting an animated WebP file to GIF.

First, we used ImageMagick, and then employed Python with the Pillow library. Finally, we created a Bash script to automatically convert all animated WebP files in the current directory to GIFs by combining shell commands and Python.