Skip to content

Instantly share code, notes, and snippets.

@cypnk
Last active January 22, 2021 20:13
Show Gist options
  • Save cypnk/a285b518753f8ac591e4df64932e4ea0 to your computer and use it in GitHub Desktop.
Save cypnk/a285b518753f8ac591e4df64932e4ea0 to your computer and use it in GitHub Desktop.
Convert .webp files to another image type (defaults to .png)
#!/bin/sh
# This snippet converts .webp images into another image format
# (.jpg, .png etc...)
#
# This assumes you already have libwebp installed
# The converted files are stored in the same folder unless another is given
#
# Usage:
# sh webpconvert.sh /path/to/files
#
# Or a different extension:
# sh webpconvert.sh /path/to/files jpg
#
# Different destination (creates the folder if it doesn't exist):
# sh webpconvert.sh /path/to/files png /destination/path
# Source folder of all your .webp images
SRC=$1
if [ ! -d "$SRC" ]; then
echo "Source directory needed"
exit 1
fi
# Preferred output extension (defaults to png)
EXT=${2:-png}
# Remove extra dots and convert to lowercase
EXT=$(echo ".${EXT//.}" | tr '[:upper:]' '[:lower:]')
# Destination (defaults to source)
DST=$(realpath ${3:-$SRC})
DST="${DST%/}/"
# All webp
FLD="${SRC%/}/*.webp"
mkdir -p $DST
for FILE in $FLD; do
if [ -f "$FILE" ]; then
# Swap out source for destination and add extension
OUT=${FILE#$SRC}
OUT="$DST${OUT%.webp}$EXT"
# Avoid overwriting
if [ -f "$OUT" ]; then
echo "File $OUT already exists. Skipping."
else
dwebp "$FILE" -o "$OUT"
fi
fi
done
echo "Conversion complete"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment