Skip to content

Instantly share code, notes, and snippets.

@schappim
Created March 18, 2026 00:08
Show Gist options
  • Select an option

  • Save schappim/b773ca97b2aebc0ea5daec242b0fb84b to your computer and use it in GitHub Desktop.

Select an option

Save schappim/b773ca97b2aebc0ea5daec242b0fb84b to your computer and use it in GitHub Desktop.
convert_to_webp.sh
alias webpconvert='convert_to_webp'
convert_to_webp() {
original_path="$1"
output_path="${original_path%.*}.webp"
extension="${original_path##*.}"
extension_lower=$(echo "$extension" | tr '[:upper:]' '[:lower:]')
if [[ "$extension_lower" == "heic" || "$extension_lower" == "heif" ]]; then
# Convert HEIC/HEIF to temporary JPEG first, then to WebP
temp_file=$(mktemp /tmp/webp_convert.XXXXXX.jpg)
sips -s format jpeg "$original_path" --out "$temp_file" >/dev/null 2>&1
cwebp -q 70 "$temp_file" -o "$output_path"
rm "$temp_file"
else
cwebp -q 70 "$original_path" -o "$output_path"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment