Improved finding new photos and converting them to lower-res version

Was not working well at all before.
Forums online suggested not using for-loop for this kind of job,
so I've replaced those with find -exec statements. Initial tests
suggest this is much more robust. Also fewer lines.
master
Taha Ahmed 4 years ago
parent c7d7b1db3c
commit e467fb74df

@ -193,44 +193,23 @@ if [ $# -eq 1 ]; then
rsync -av "$path_thesis/assets/photos/" "$path_thesis/assets/photos/.lowres/" --exclude ".lowres/"
touch "$photoslastrun"
fi
# cd and use --parents arg to preserve directory structure in .lowres target
# Detect new photos and copy them into .lowres tree
# https://stackoverflow.com/questions/9612090/how-to-loop-through-file-names-returned-by-find
# https://stackoverflow.com/questions/5241625/find-and-copy-files
cd "$path_thesis/assets/photos"
newphotos="$(find . -type f -cnewer $photoslastrun ! -path './.lowres/*')"
if [ -n "$newphotos" ]; then
for newphoto in $newphotos; do
cp --parents $newphoto .lowres/
done
fi
find . -type f -cnewer $photoslastrun ! -path "./.lowres/*" -print -exec cp --parents "{}" .lowres \;
# revert the effects of cd above. Redirect to null suppresses the output.
cd - >/dev/null
# in the low-res tree, find any photo larger than specific size (500kB)
largephotos="$(find $path_thesis/assets/photos/.lowres/ -size +500k)"
for largephotofilename in $largephotos; do
# for the next statement to work reliably, we should probably convert other formats to JPEG
# detect file extension, and based on it, convert to jpg using mogrify
largephotobase=$(basename -- "$largephotofilename") # just the filename (with extension, sans parents)
largephototype=${largephotobase#*.} # file extension only
largephotoname=${largephotofilename%.*} # path without extension
largephotobasename=${largephotobase%.*} # basename without extension
# some filetypes don't fare well when converted to jpeg, so we will only run the
# forced conversion and shrinking unless the file extension is one of the following:
# "" (empty string, ie no file extension)
# SVG
# PDF
if [ ! "$largephototype" == "" ] || [ ! "$largephototype" == "svg" ] || [ ! "$largephototype" == "SVG" ] || [ ! "$largephototype" == "pdf" ] || [ ! "$largephototype" == "PDF" ]; then
# if the photo is not already jpeg, convert it to jpeg
if [ ! "$largephototype" == "jpg" ] && [ ! "$largephototype" == "jpeg" ] && [ ! "$largephototype" == "JPG" ] && [ ! "$largephototype" == "JPEG" ]; then
echo "<thesis> Converting $largephotobase to JPG format"
mogrify -format jpg $largephotofilename
# remove the now unnecessary non-jpg file from .lowres/
rm "$largephotofilename"
fi
# convert photo in-place (overwrite) with new one roughly 300kb in size
# https://stackoverflow.com/questions/6917219/imagemagick-scale-jpeg-image-with-a-maximum-file-size
echo "<thesis> Shrinking $largephotobasename.jpg"
convert $largephotoname.jpg -define jpeg:extent=300kb $largephotoname.jpg
fi
done
# Convert all non-JPG images (except for PDF, SVG, and other non-images) to JPG
echo "<thesis> Convert all non-JPG images to JPG"
find "$path_thesis/assets/photos/.lowres/" -type f ! -name "*.pdf" ! -name "*.svg" ! -name "*.external" ! -name "lastrun" ! -name "*.jpg" -print -exec mogrify -format jpg "{}" \;
# Remove the now redundant non-JPG files in .lowres/
echo "<thesis> Delete the redundant non-JPG files in .lowres/ tree"
find "$path_thesis/assets/photos/.lowres/" -type f ! -name "*.pdf" ! -name "*.svg" ! -name "*.external" ! -name "lastrun" ! -name "*.jpg" -print -exec rm "{}" \;
# Shrink image filesize in-place to roughly 300kb in size
echo "<thesis> Shrink images in .lowres/ tree to <=300K"
# https://stackoverflow.com/questions/6917219/imagemagick-scale-jpeg-image-with-a-maximum-file-size
find "$path_thesis/assets/photos/.lowres/" -size +500k -type f -name "*.jpg" -print -exec convert "{}" -define jpeg:extent=300kb "{}" \;
# update the modification and access time on the photosastrun file
touch "$photoslastrun"

Loading…
Cancel
Save