How to Convert WEBP Files to PNG Using VS Code in Python
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:
-
Python Installed
If you don’t have Python installed, download it from python.org and install it. -
VS Code Installed
Download and install Visual Studio Code from code.visualstudio.com. -
Pillow Library
We will use thePillow
(PIL) library to handle image conversions. Install it using:pip install pillow
Step 1: Set Up Your Python Project in VS Code
- Open VS Code
- Create a New Folder and name it
image_converter
- Open the Folder in VS Code (
File > Open Folder > Select your folder
) - Create a Python Script
Inside your folder, create a file namedconvert.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 thepng_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:
- Inside the
image_converter
folder, create a new folder named webp_images. - Place your
.webp
images inside this folder.
Step 4: Running the Python Script in VS Code
-
Open VS Code Terminal (
View > Terminal
). -
Navigate to your project folder:
cd path/to/image_converter
-
Run the script:
python convert.py
-
If successful, you will see output like:
Converted image1.webp to image1.png Converted image2.webp to image2.png Conversion complete!
-
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.