In this tutorial, we will introduce you how to convert a webp image to png using python pillow. This example is very useful if you have got some webp image from internet.
Preliminary
We can import python pillow library firstly.
from PIL import Image
Read a webp image
In order to convert webp to png, we should read a webp image data.
Here is an example:
image_webp = 'chart.webp' image_png = 'chart.png' im = Image.open(image_webp)
Then we can start to convert.
Convert webp to png
im.save(image_png, format="png", lossless=True)
In this example code, we will use im.save() function to convert.
However, we should notice: the png image is bigger than webp. Here is the comparison.
chart.web | 2.67 KB |
chart.png | 8.39 KB |
If you want to convert png to webp, you can read:
Best Practice to Python Convert PNG to WebP with Pillow – Python Pillow Tutorial