Skip to content

Instantly share code, notes, and snippets.

@VIRUXE
Created May 11, 2024 23:15
Show Gist options
  • Save VIRUXE/daae80cdf192079af2411a371bd98f7b to your computer and use it in GitHub Desktop.
Save VIRUXE/daae80cdf192079af2411a371bd98f7b to your computer and use it in GitHub Desktop.
Convert PNG images to WebP, using Pillow
import os
from PIL import Image
import sys
for root, dirs, files in os.walk(sys.argv[1] if len(sys.argv) > 1 else "."):
for file in files:
if file.endswith('.png'):
png_path = os.path.join(root, file)
webp_path = png_path.replace('.png', '.webp')
Image.open(png_path).convert("RGBA").save(webp_path, "WEBP", quality=100)
print(f"{png_path} -> {webp_path}")
if not '--recursive' in sys.argv: break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment