Skip to content

Instantly share code, notes, and snippets.

@dudor
Last active January 24, 2022 05:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dudor/979378c18dc07e7b599c737c6120bd9b to your computer and use it in GitHub Desktop.
Save dudor/979378c18dc07e7b599c737c6120bd9b to your computer and use it in GitHub Desktop.
CONVERT JPG , PNG TO WEBP
#!/usr/bin/python3
import sys
import subprocess
import os
def cwebp(dir):
for item in os.listdir(dir):
file = os.path.join(dir,item)
if(os.path.isfile(file)):
ext =os.path.splitext(file)
if(os.path.getsize(file) > 100000 and (ext[1].lower()==".jpg" or ext[1].lower()==".png")):
#print(ext)
r=subprocess.call(["cwebp","-metadata","all","-q","90","-mt",file,"-o",file+".webp"])
print(r)
os.remove(file)
if(os.path.isdir(file)):
print(file)
cwebp(file)
cwebp(".")
#!/bin/bash
cwebp_img(){
for f in $1"/"*;do
#echo "$f"
#cwebp -q 90 "$f" -o "$f.webp"
if [ -d "$f" ]
then
#cwebp_img "$1/$f"
echo "$f"
cwebp_img "$f"
else
filesize=`ls -l "$f" | awk '{ print $5 }'`
if [ $filesize -gt 204800 ]
then
if [ "${f##*.}"x = "jpg"x ]||[ "${f##*.}"x = "png"x ]||[ "${f##*.}"x = "JPG"x ]||[ "${f##*.}"x = "PNG"x ];then
cwebp -mt -q 85 -metadata all "$f" -o "$f.webp"
#echo "$f.webp"
rm "$f"
fi
fi
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment