How to convert images to webp with Python?

Let's convert a bunch of images to WebP format with the Help of a simple Python script which less than 10 lines of code!

How to convert images to webp with Python?

In my recent blog post, I showed how to convert and compress large image files into small WebP files on Windows. You can find the full post here

I'm assuming you have some knowledge about Python and it's installed on your computer. If you don't, you can download it from here.

Project structure

Let's create a folder named WebP Converter. You can name it whatever you want. Inside that folder, create two folders: in and out, and one main.py file.

Visit WebP downloads repository from here

Scroll down to the very bottom for the latest releases. Find the latest version of Windows. It should be named like this libwebp-x.x.x-windows-x64. Avoid libwebp-x.x.x-windows-x64-no-wic file and .asc extentions. At the time I'm writing this, the latest version is 1.2.2 release at 2022-01-20T02:44:25Z.

Copy the bin folder and paste it in the root of the project folder.

The project structure should look like this:

📦WebP Converter
 ┣ 📂bin
 ┃ ┣ 📜anim_diff.exe
 ┃ ┣ 📜anim_dump.exe
 ┃ ┣ 📜cwebp.exe
 ┃ ┣ 📜dwebp.exe
 ┃ ┣ 📜freeglut.dll
 ┃ ┣ 📜get_disto.exe
 ┃ ┣ 📜gif2webp.exe
 ┃ ┣ 📜img2webp.exe
 ┃ ┣ 📜vwebp.exe
 ┃ ┣ 📜webpinfo.exe
 ┃ ┣ 📜webpmux.exe
 ┃ ┗ 📜webp_quality.exe
 ┣ 📂in
 ┣ 📂out
 ┗ 📜main.py

Testing

Let's test if our project is working on command line. If so, we can proceed to the next step. I've placed a sample cat.jpg image in in folder.

First, we need to access the cwebp.exe file inside of bin folder. We can do that by typing "bin/cwebp.exe" in the command line.

then we need to specify the input file. We can do that by typing "in/sample.jpg" in the command line.

Finally, we need to specify the output file. We can do that by typing "out/sample.webp" in the command line.

So, the fill command line should look like this:

>> "bin/cwebp.exe" "in/cat.jpg" -o "out/cat.webp"

Run the command line and see if it works. If it works you'll see a message log like this:

Saving file 'out/cat.webp'
File:      in/cat.jpg
Dimension: 3374 x 4498
Output:    352798 bytes Y-U-V-All-PSNR 44.41 49.99 50.20   45.62 dB
           (0.19 bpp)
block count:  intra4:      15253  (25.63%)
              intra16:     44249  (74.37%)
              skipped:     26283  (44.17%)
bytes used:  header:            304  (0.1%)
             mode-partition:  78440  (22.2%)
 Residuals bytes  |segment 1|segment 2|segment 3|segment 4|  total
    macroblocks:  |       1%|       8%|       8%|      83%|   59502
      quantizer:  |      36 |      36 |      33 |      23 |
   filter level:  |      11 |       9 |      42 |      35 |

Python script

Now that our testing went successfully, we can start coding. Open the main.py file with your favorite code editor. I'll be using VS Code.

Let's import two libraries/modules: os and subprocess.

import os
import subprocess

We can use os.listdir() to get a list of files in a folder. Let's get a list of all the files in the in folder.

img_list = os.listdir("in/")

Now we can loop over img_list and convert each image to WebP format. We can use subprocess.Popen() to run a command line command inside the loop. We see what the command line command should look like.

for i in img_list:
    cmd = f'"bin/cwebp.exe" "in/{i}" -o "out/{i.split(".")[0]}.webp"'
    subprocess.Popen(cmd, shell=True)

We are done! It's only 6 lines of code! The full code should look like this:

import os
import subprocess

img_list = os.listdir("in/")

for i in img_list:
    cmd = f'"bin/cwebp.exe" "in/{i}" -o "out/{i.split(".")[0]}.webp"'
    subprocess.Popen(cmd, shell=True)

Let's check if the script is working

Place some images inside of the in folder. We need to run the main.py file. We can do that by typing python main.py in the command line.

>> python main.py

If the code is working, you'll see a terminal pop up with some logs. Once it's done, go to the out folder and see if you have any WebP files there.

📦WebP Converter
 ┣ 📂bin
 ┃ ┣ 📜anim_diff.exe
 ┃ ┣ 📜anim_dump.exe
 ┃ ┣ 📜cwebp.exe
 ┃ ┣ 📜dwebp.exe
 ┃ ┣ 📜freeglut.dll
 ┃ ┣ 📜get_disto.exe
 ┃ ┣ 📜gif2webp.exe
 ┃ ┣ 📜img2webp.exe
 ┃ ┣ 📜vwebp.exe
 ┃ ┣ 📜webpinfo.exe
 ┃ ┣ 📜webpmux.exe
 ┃ ┗ 📜webp_quality.exe
 ┣ 📂in
 ┃ ┗ 📜Cat.jpg
 ┣ 📂out
 ┃ ┗ 📜cat.webp
 ┗ 📜main.py

Congratulations! You've finished the WebP Converter project. You've made a script that can convert thousands of images to WebP format at once.

Feel free to share this project with your friends. Leave a comment if you have any questions or suggestions. If you're stuck on the project, you can always ask me a question on the GitHub repo of this project. I'll be happy to help you.

Views:-

Source Code: https://github.com/Tarikul-Islam-Anik/WebP-Converter

Language: Python

Topics: Webp, Python Script