Skip to content

Instantly share code, notes, and snippets.

@M-Drummond
Last active March 14, 2024 04:17
Show Gist options
  • Save M-Drummond/3d95304616b9ba5ab879ec58e842a947 to your computer and use it in GitHub Desktop.
Save M-Drummond/3d95304616b9ba5ab879ec58e842a947 to your computer and use it in GitHub Desktop.
Python: Convert WebP All Images In Dir To WebP
#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