Skip to content

Instantly share code, notes, and snippets.

@s-gbz
Created October 24, 2020 18:36
Show Gist options
  • Save s-gbz/9710d3c3469202996925e6420e3010ee to your computer and use it in GitHub Desktop.
Save s-gbz/9710d3c3469202996925e6420e3010ee to your computer and use it in GitHub Desktop.
Auto delete, resize, convert & rename images. The script is to be executed with a linux bash and can be expanded to make further use of variables (e.g. generate various resolution)
# The script expects a directory structure like this:
# . (Execute the script from this directory)
# - /originals (Original images to be processed)
# - /fulls (Copies with reduced, but better quality to be displayed in "full size" and only loaded on click)
# - /thumbnails (Copies with lower quality than "fulls". Used as gallery thumbnails and thus loaded first)
# - /tiny-thumbnails (Copies with minimal resolution. Used as navigation thumbnails inside a photo viewer)
# Used in production at: https://grilborzer.de
# GitHub: https://github.com/s-gbz/grilborzer.github.io
DIRECTORY_ORIGINALS=./originals
DIRECTORY_FULL_SIZE=./fulls
DIRECTORY_THUMBNAILS=./thumbnails
DIRECTORY_TINY_THUMBNAILS=./tiny-thumbnails
# NOTE: Bash execution on windows machines STILL REQUIRES to use a webpc WINDOWS executable
# Encoder taken from https://developers.google.com/speed/webp/download
DIRECTORY_WEBP_ENCODER_BIN=./libwebp-1.0.3-windows-x64/bin/cwebp
QUALITY_WEBP_OUTPUT_FULL_SIZE=95
QUALITY_WEBP_OUTPUT_THUMBNAILS=75
RESIZE_FACTOR_FULL_SIZE=25
RESIZE_FACTOR_THUMBNAILS=20
RESIZE_FACTOR_TINY_THUMBNAILS=1
echo "\nDelete existing output directories (if existing)"
rm -r $DIRECTORY_FULL_SIZE
echo "\ - Deleted $DIRECTORY_FULL_SIZE"
rm -r $DIRECTORY_THUMBNAILS
echo "\ - Deleted $DIRECTORY_FULL_SIZE"
rm -r $DIRECTORY_TINY_THUMBNAILS
echo "\ - Deleted $DIRECTORY_FULL_SIZE"
echo "\n(Re-)Create output directories"
mkdir $DIRECTORY_FULL_SIZE
echo "\ - Created $DIRECTORY_FULL_SIZE"
mkdir $DIRECTORY_THUMBNAILS
echo "\ - Created $DIRECTORY_THUMBNAILS"
mkdir $DIRECTORY_TINY_THUMBNAILS
echo "\ - Created $DIRECTORY_TINY_THUMBNAILS"
echo "\nResize originals to smaller .jpeg files"
magick convert $DIRECTORY_ORIGINALS/*.jpg -resize $RESIZE_FACTOR_FULL_SIZE% $DIRECTORY_FULL_SIZE/%01d.jpg
echo "\ - Resized to $DIRECTORY_ORIGINALS"
magick convert $DIRECTORY_ORIGINALS/*.jpg -resize 25% $DIRECTORY_THUMBNAILS/%01d.jpg
echo "\ - Resized to $DIRECTORY_THUMBNAILS"
magick convert $DIRECTORY_ORIGINALS/*.jpg -resize $RESIZE_FACTOR_TINY_THUMBNAILS% $DIRECTORY_TINY_THUMBNAILS/%01d.gif
echo "\ - Resized to $DIRECTORY_TINY_THUMBNAILS"
echo "\nConvert .jpeg images to a .webp copy"
for image in $DIRECTORY_FULL_SIZE/*.jpg; do
$DIRECTORY_WEBP_ENCODER_BIN $image -q $QUALITY_WEBP_OUTPUT_FULL_SIZE -o $image.webp -quiet
done
echo "\ - nConverted to .webp in $DIRECTORY_FULL_SIZE"
for image in $DIRECTORY_THUMBNAILS/*.jpg; do
$DIRECTORY_WEBP_ENCODER_BIN $image -q $QUALITY_WEBP_OUTPUT_THUMBNAILS -o $image.webp -quiet
done
echo "\ - nConverted to .webp in $DIRECTORY_THUMBNAILS"
echo "\nRename .jpg.webp images to .webp"
for image in $DIRECTORY_FULL_SIZE/*.jpg.webp; do
mv "$image" "$DIRECTORY_FULL_SIZE/$(basename "$image" .jpg.webp).webp"
done
echo "\ - Renamed .jpg.webp to .webp in $DIRECTORY_FULL_SIZE"
for image in $DIRECTORY_THUMBNAILS/*.jpg.webp; do
mv "$image" "$DIRECTORY_THUMBNAILS/$(basename "$image" .jpg.webp).webp"
done
echo "\ - Renamed .jpg.webp to .webp in $DIRECTORY_THUMBNAILS"
echo "Press ENTER to close"
read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment