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/" rsync -av "$path_thesis/assets/photos/" "$path_thesis/assets/photos/.lowres/" --exclude ".lowres/"
touch "$photoslastrun" touch "$photoslastrun"
fi 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" cd "$path_thesis/assets/photos"
newphotos="$(find . -type f -cnewer $photoslastrun ! -path './.lowres/*')" find . -type f -cnewer $photoslastrun ! -path "./.lowres/*" -print -exec cp --parents "{}" .lowres \;
if [ -n "$newphotos" ]; then
for newphoto in $newphotos; do
cp --parents $newphoto .lowres/
done
fi
# revert the effects of cd above. Redirect to null suppresses the output. # revert the effects of cd above. Redirect to null suppresses the output.
cd - >/dev/null cd - >/dev/null
# in the low-res tree, find any photo larger than specific size (500kB) # Convert all non-JPG images (except for PDF, SVG, and other non-images) to JPG
largephotos="$(find $path_thesis/assets/photos/.lowres/ -size +500k)" echo "<thesis> Convert all non-JPG images to JPG"
for largephotofilename in $largephotos; do find "$path_thesis/assets/photos/.lowres/" -type f ! -name "*.pdf" ! -name "*.svg" ! -name "*.external" ! -name "lastrun" ! -name "*.jpg" -print -exec mogrify -format jpg "{}" \;
# for the next statement to work reliably, we should probably convert other formats to JPEG # Remove the now redundant non-JPG files in .lowres/
# detect file extension, and based on it, convert to jpg using mogrify echo "<thesis> Delete the redundant non-JPG files in .lowres/ tree"
largephotobase=$(basename -- "$largephotofilename") # just the filename (with extension, sans parents) find "$path_thesis/assets/photos/.lowres/" -type f ! -name "*.pdf" ! -name "*.svg" ! -name "*.external" ! -name "lastrun" ! -name "*.jpg" -print -exec rm "{}" \;
largephototype=${largephotobase#*.} # file extension only # Shrink image filesize in-place to roughly 300kb in size
largephotoname=${largephotofilename%.*} # path without extension echo "<thesis> Shrink images in .lowres/ tree to <=300K"
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 # https://stackoverflow.com/questions/6917219/imagemagick-scale-jpeg-image-with-a-maximum-file-size
echo "<thesis> Shrinking $largephotobasename.jpg" find "$path_thesis/assets/photos/.lowres/" -size +500k -type f -name "*.jpg" -print -exec convert "{}" -define jpeg:extent=300kb "{}" \;
convert $largephotoname.jpg -define jpeg:extent=300kb $largephotoname.jpg
fi
done
# update the modification and access time on the photosastrun file # update the modification and access time on the photosastrun file
touch "$photoslastrun" touch "$photoslastrun"

Loading…
Cancel
Save