WebP format is a good choice for displaying images on website. WebP lossless images are 26% smaller in size compared to PNGs. WebP lossy images are 25-34% smaller than comparable JPEG images at equivalent SSIM quality index. In this tutorial, we will introduce how to convert png to webp image with python, you also can convert other formats to webp by following our tutorial.
Prerequisite
from PIL import Image
Set a png image
image_old = 'e:\\1.png' image_new = 'e:\\1.webp'
Convert png to webp format
im = Image.open(image_old) im.save(image_new, format = "WebP", lossless = True)
Notice: you must set lossless = True, otherwise the size of webp may be bigger than png.
Compare size
1.png 92.0 KB
1.webp 36.0 KB
Saving about 60% disk space.