Convert WebP to PNG with Python and Pillow
Convert a still WebP to PNG, keeping its dimensions and transparency.
View code and setup
Before you start
- Install Python 3 with virtual-environment support.
- Put a copy of your image in a working folder. Use the input filename shown in the script, or change it to match your file.
- Run the setup commands for your operating system to install Pillow in a local .venv.
- Save the Python code as convert_image.py in the same folder, then run it with the command for your operating system.
- The script saves a new image. Change the output filename before running if you want to keep an earlier result.
python3 -m venv .venv
.venv/bin/python -m pip install Pillowpy -3 -m venv .venv
.venv\Scripts\python.exe -m pip install Pillowfrom PIL import Image
with Image.open("input.webp") as image:
if getattr(image, "n_frames", 1) != 1:
raise ValueError("Animated WebP is outside this example")
image.convert("RGBA").save("converted.png").venv/bin/python convert_image.py.venv\Scripts\python.exe convert_image.pyLimits and output
- Requires Pillow with WebP decoding support. This example rejects animation and keeps the decoded dimensions.
- PNG can be larger. It cannot restore detail lost in the original WebP; metadata and colour profiles are not explicitly carried over.
Documentation: Pillow image operations; Pillow tutorial and installation