From e467fb74df1ed9531fafea9707101b1d4c42a1e5 Mon Sep 17 00:00:00 2001 From: "taha@asks2" Date: Tue, 13 Oct 2020 03:37:15 +0200 Subject: [PATCH] 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. --- chertex.sh | 49 ++++++++++++++----------------------------------- 1 file changed, 14 insertions(+), 35 deletions(-) diff --git a/chertex.sh b/chertex.sh index 768f980..505f57c 100755 --- a/chertex.sh +++ b/chertex.sh @@ -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 " 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 " 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 " 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 " 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 " 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"