Last active
March 14, 2024 04:17
-
-
Save M-Drummond/3d95304616b9ba5ab879ec58e842a947 to your computer and use it in GitHub Desktop.
Python: Convert WebP All Images In Dir To WebP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#run via: $ python3 webp.py | |
import os | |
# Present working directory | |
directory = "." | |
# Loop through each file | |
for file in os.listdir(directory): | |
if os.path.isfile(os.path.join(directory, file)): | |
filename, extension = os.path.splitext(file) | |
# Check if the file is an image | |
if extension.lower() in [".jpg", ".jpeg", ".png", ".gif"]: | |
# Convert the image to WebP format | |
os.system(f"cwebp \"{os.path.join(directory, file)}\" -o \"{os.path.join(directory, filename)}.webp\"") | |
print(f"Converted {file} to WebP") | |
else: | |
print(f"Skipping {file}, not an image file") | |
print("Conversion complete") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment