Thesis: create low-res photos on-the-fly from existing photos

master
Taha Ahmed 6 years ago
parent 93fbb4586f
commit fb97a2acc8

@ -84,8 +84,8 @@ if [ $# -eq 1 ]; then
fi
#### Special treatment for thesis
# If $jobname is thesis, fetch some external assets
if [[ ${PWD} == "/media/bay/taha/chepec/thesis" && $jobname == "thesis" ]]; then
# Fetch external assets by reading any assets.external files in assets/ tree
echo "<thesis> -------------------------------"
echo "<thesis> Getting external assets"
echo "<thesis> -------------------------------"
@ -108,9 +108,58 @@ if [ $# -eq 1 ]; then
done < "$assets"
fi
done
# Create low-res photos on-the-fly from existing photos/
echo "<thesis> -------------------------------"
echo "<thesis> Create low-res photos tree"
echo "<thesis> -------------------------------"
# copy existing photos to assets/photos/.lowres/ path
# to save time, rsync only if highres photo has more recent timestamp (otherwise, keep lowres photo without overwriting)
# Note: rsync usually looks at file timestamp and size, and if either has changed, copies the file (simplified explanation)
# in this case, I'd like rsync to only compare timestamps and disregard size
# rsync can't do that. We need to use a different tool. See e.g.
# https://superuser.com/questions/260092/rsync-switch-to-only-compare-timestamps
# copy only the "large" photos that have file modtimes more recent than the last time this operation was run
photoslastrun="/media/bay/taha/chepec/thesis/assets/photos/.lowres/lastrun"
if [ ! -f "$photoslastrun" ]; then
# if, for some reason, the lastrun file does not exist
# copy over everything and then create the file
rsync -av /media/bay/taha/chepec/thesis/assets/photos/* /media/bay/taha/chepec/thesis/assets/photos/.lowres/ --exclude /media/bay/taha/chepec/thesis/assets/photos/.lowres/
touch "$photoslastrun"
fi
# cd and use --parents arg to preserve directory structure in .lowres target
cd /media/bay/taha/chepec/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
# cd - also creates output (the current wd). Redirect to null suppresses the output.
cd - >/dev/null
fi
# in the low-res tree, find any photo larger than specific size (500kB)
largephotos="$(find /media/bay/taha/chepec/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
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
done
fi
# short delay to enable on-screen reading of previous echo
simpledelay.sh 1
simpledelay.sh 2
## Handle knitr or pgfSweave jobs (each requires separate treatment)

Loading…
Cancel
Save