If you have a set of .webp images that need to be converted to .png, Python makes the process simple and efficient. In this guide, I’ll walk you through how to convert WebP files to PNG using Visual Studio Code (VS Code) and Python.

Prerequisites

Before we begin, ensure you have the following:

  1. Python Installed
    If you don’t have Python installed, download it from python.org and install it.

  2. VS Code Installed
    Download and install Visual Studio Code from code.visualstudio.com.

  3. Pillow Library
    We will use the Pillow (PIL) library to handle image conversions. Install it using:

    pip install pillow
    

Step 1: Set Up Your Python Project in VS Code

  1. Open VS Code
  2. Create a New Folder and name it image_converter
  3. Open the Folder in VS Code (File > Open Folder > Select your folder)
  4. Create a Python Script
    Inside your folder, create a file named convert.py.

Step 2: Writing the Python Script to Convert WebP to PNG

Now, open convert.py and add the following code:

from PIL import Image
import os

# Define the input and output directories
input_folder = "webp_images"
output_folder = "png_images"

# Ensure output folder exists
os.makedirs(output_folder, exist_ok=True)

# Process each WebP file
for filename in os.listdir(input_folder):
    if filename.endswith(".webp"):
        # Open the WebP image
        img = Image.open(os.path.join(input_folder, filename))
        
        # Convert to PNG and save
        png_filename = os.path.splitext(filename)[0] + ".png"
        img.save(os.path.join(output_folder, png_filename), "PNG")
        
        print(f"Converted {filename} to {png_filename}")

print("Conversion complete!")

How This Works:

  • The script looks for .webp files inside a folder named webp_images.
  • It creates an output folder called png_images if it doesn’t exist.
  • It converts each .webp file to .png and saves it inside the png_images folder.
  • The script prints each conversion status to the terminal.

Step 3: Preparing Your Image Folders

Before running the script, you need to organize your images:

  1. Inside the image_converter folder, create a new folder named webp_images.
  2. Place your .webp images inside this folder.

Step 4: Running the Python Script in VS Code

  1. Open VS Code Terminal (View > Terminal).

  2. Navigate to your project folder:

    cd path/to/image_converter
    
  3. Run the script:

    python convert.py
    
  4. If successful, you will see output like:

    Converted image1.webp to image1.png
    Converted image2.webp to image2.png
    Conversion complete!
    
  5. Check the png_images folder. You’ll find your converted PNG files there!


Step 5: Automating Multiple Image Conversions

If you want to process images in different directories or accept user input, modify the script like this:

input_folder = input("Enter the path to the folder with WebP images: ")
output_folder = input("Enter the path to save PNG images: ")

# Ensure output folder exists
os.makedirs(output_folder, exist_ok=True)

for filename in os.listdir(input_folder):
    if filename.endswith(".webp"):
        img = Image.open(os.path.join(input_folder, filename))
        png_filename = os.path.splitext(filename)[0] + ".png"
        img.save(os.path.join(output_folder, png_filename), "PNG")
        print(f"Converted {filename} to {png_filename}")

print("All conversions completed!")

Now, when you run the script, you can enter custom input and output folders.