Skip to content

Instantly share code, notes, and snippets.

@icefo
Created April 28, 2020 13:05
Show Gist options
  • Save icefo/7b8eda5ba749f1f39e3daeb69a39abfe to your computer and use it in GitHub Desktop.
Save icefo/7b8eda5ba749f1f39e3daeb69a39abfe to your computer and use it in GitHub Desktop.
#!/bin/bash
#Author: Poul Serek
# Modified by Adrien Remillieux
shopt -s globstar
set -euo pipefail
echo "Starting Piwigo thumbnail generation"
sourceDir="/source/path"
destDir="/dest/path"
counter=0
fnNoExt=""
fnExt=""
fnPath=""
STARTTIME=$(date +%s)
for file in "$sourceDir"/**/*.{jpg,JPG,jpeg,JPEG}
do
if [[ ! -f "$file" ]]
then
continue
fi
fnNoExt="${file%.*}"
fnExt="${file##*.}"
fnPath="${file%/*}"
fnPath="${fnPath#$sourceDir}"
fnNoExt="${fnNoExt#$sourceDir}"
echo "${fnNoExt}"
mkdir -p "${destDir}${fnPath}"
#Error checking
result=$(jpeginfo -c "$file")
if [[ $result != *"[OK]"* ]]
then
echo $result
fi
#If the medium thumbnail exists we assume that the rest also exists and skip this image
if [ ! -f "${destDir}${fnNoExt}-me.${fnExt}" ]; then
#echo "MISSING! ${destDir}${fnNoExt}-me.${fnExt}"
#Store correctly oriented base image (medium) in memory. All other thumbnails are created from this
convert "${file}" -auto-orient -write \
mpr:baseline -thumbnail 240x240 -write "${destDir}${fnNoExt}-2s.${fnExt}" +delete \
mpr:baseline -resize 520x360 -write "${destDir}${fnNoExt}-cu_e520x360.${fnExt}" +delete \
mpr:baseline -thumbnail 250x250^ -gravity center -extent 250x250 -write "${destDir}${fnNoExt}-cu_e250.${fnExt}" +delete \
mpr:baseline -resize 1008x756 -write "${destDir}${fnNoExt}-la.${fnExt}" +delete \
mpr:baseline -resize 792x594 -write "${destDir}${fnNoExt}-me.${fnExt}" +delete \
mpr:baseline -resize 576x432 -write "${destDir}${fnNoExt}-sm.${fnExt}" +delete \
mpr:baseline -resize 144x144 -write "${destDir}${fnNoExt}-th.${fnExt}" +delete \
mpr:baseline -resize 1224x918 -write "${destDir}${fnNoExt}-xl.${fnExt}" +delete \
mpr:baseline -resize 432x324 -write "${destDir}${fnNoExt}-xs.${fnExt}" +delete \
mpr:baseline -resize 1656x1242 -write "${destDir}${fnNoExt}-xx.${fnExt}" +delete \
mpr:baseline -thumbnail 120x120^ -gravity center -extent 120x120 "${destDir}${fnNoExt}-sq.${fnExt}"
fi
counter=$[$counter +1]
if [ $(($counter%100)) -eq 0 ]; then
ENDTIME=$(date +%s)
echo "Processed: ${counter} - Executing for $((($ENDTIME - $STARTTIME)/60)) minutes"
fi
done
ENDTIME=$(date +%s)
echo "It took $((($ENDTIME - $STARTTIME)/60)) minutes to complete this task..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment