Created
June 12, 2026 14:07
-
-
Save vossisboss/7fbd3444e9e55a271b7cdc1e0da9e79e to your computer and use it in GitHub Desktop.
Example script for MacOS for converting static images to the webp format
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
| #!/bin/bash | |
| # convert-to-webp.sh | |
| # Converts PNG/JPG/GIF/TIFF/BMP to WebP. Animated GIFs use gif2webp. Does not delete originals. The .webp versions are written alongside them. | |
| # Skips: favicons, SVG, already-WebP files. | |
| # Replace IMG_DIR file path from your project to use this script for your own stuff. This script should work on Linux as well but will need some modification for Windows. | |
| IMG_DIR="${1:-wagtailio/static/img}" | |
| QUALITY=85 | |
| METHOD=6 | |
| find "$IMG_DIR" -type f \( \ | |
| -name "*.png" -o -name "*.jpg" -o -name "*.jpeg" \ | |
| -o -name "*.gif" -o -name "*.tiff" -o -name "*.tif" \ | |
| -o -name "*.bmp" \ | |
| \) ! -path "*/favicons/*" \ | |
| | while read -r src; do | |
| out="${src%.*}.webp" | |
| echo "Converting: $src → $out" | |
| src_lower=$(echo "$src" | tr '[:upper:]' '[:lower:]') | |
| case "$src_lower" in | |
| *.jpg|*.jpeg) | |
| cwebp -q $QUALITY -m $METHOD -metadata none "$src" -o "$out" | |
| ;; | |
| *.png) | |
| cwebp -q $QUALITY -m $METHOD -sharp_yuv -alpha_q 90 "$src" -o "$out" | |
| ;; | |
| *.gif) | |
| frames=$(identify -format "%n\n" "$src" 2>/dev/null | head -1) | |
| if [[ "$frames" -gt 1 ]] 2>/dev/null; then | |
| gif2webp -q $QUALITY -m $METHOD -mixed -loop_compatibility "$src" -o "$out" | |
| else | |
| cwebp -q $QUALITY -m $METHOD "$src" -o "$out" | |
| fi | |
| ;; | |
| *.tiff|*.tif) | |
| cwebp -q 90 -m $METHOD -metadata none "$src" -o "$out" | |
| ;; | |
| *.bmp) | |
| cwebp -q $QUALITY -m $METHOD -sharp_yuv "$src" -o "$out" | |
| ;; | |
| esac | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment