I've been working on images for a website and find out that .webp
format is much more compact than .jpeg
or .png
find more on Docs .
Now I have a folder with almost 25 images & I want to convert all into .webp
format. Can anyone suggest me how to convert all by using just a python script without using online tools.
Well first of all you have to download the cwebp compressor tool according to your machine(Windows|Linux) from here .
Now after extracting the folder in C:\\Program Files\\
you have to set path to cwebp.exe
, below is my path Path:: C:\\Program Files\\libwebp\\bin
Open cmd to check if you have done right till now.
cmd> cwebp -version
cmd> python --version
Now it's super easy just run the below script and you have your desired output or you can download my repo on github from here
# --cwebp_compressor.py--
# cmd> python cwebp_compressor.py folder-name 80
import sys
from subprocess import call
from glob import glob
#folder-name
path = sys.argv[1]
#quality of produced .webp images [0-100]
quality = sys.argv[2]
if int(quality) < 0 or int(quality) > 100:
print("image quality out of range[0-100] ;/:/")
sys.exit(0)
img_list = []
for img_name in glob(path+'/*'):
# one can use more image types(bmp,tiff,gif)
if img_name.endswith(".jpg") or img_name.endswith(".png") or img_name.endswith(".jpeg"):
# extract images name(image_name.[jpg|png]) from the full path
img_list.append(img_name.split('\\')[-1])
# print(img_list) # for debug
for img_name in img_list:
# though the chances are very less but be very careful when modifying the below code
cmd='cwebp \"'+path+'/'+img_name+'\" -q '+quality+' -o \"'+path+'/'+(img_name.split('.')[0])+'.webp\"'
# running the above command
call(cmd, shell=False)
# print(cmd) # for debug
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.