scripts/graphics/tiff-batch-convert.sh

97 lines
2.6 KiB
Bash
Executable File

#!/bin/sh
#
# tiff-batch-convert.sh, Copyright © 2017 Matteo Cypriani
#
# This program is free software. It comes without any warranty, to the extent
# permitted by applicable law. You can redistribute it and/or modify it under
# the terms of the Do What The Fuck You Want To Public License, Version 2, as
# published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more
# details.
#
# This script batch-converts TIFF images to another format of your choice.
#
# Dependencies:
# - convert from ImageMagick
# - tiffinfo from libtiff
set -e
#set -x
######################
# USER CONFIGURATION #
######################
# Format to convert the TIFF images to (lowercase)
FORMAT="jpg"
# Maximum size of the pictures for the "low" quality setting.
LOW_SCALE="800x800"
# Maximum size of the pictures for the "medium" quality setting.
MEDIUM_SCALE="2000x2000"
#############################
# END OF USER CONFIGURATION #
#############################
# Do we have at least a quality setting, an input album and a destination
# folder?
if [ $# -lt 3 ] ; then
echo "Usage:" >&2
echo " $0 <QUALITY> <DESTINATION> <ALBUM1> [ALBUM2 [...]]" >&2
echo '<QUALITY> can be "high" (no rescaling), "medium" or "low".' >&2
echo '<DESTINATION> is the base directory in which the converted albums will be stored.' >&2
echo 'Each <ALBUM> is a directory containing TIFF files.' >&2
exit 1
fi
QUALITY="$1"
shift
ALBUM_DEST="$1"
shift
# Should the pictures be resized, depending on the quality setting?
if [ "$QUALITY" = "low" ] ; then
SCALE_OPT="-scale $LOW_SCALE"
elif [ "$QUALITY" = "medium" ] ; then
SCALE_OPT="-scale $MEDIUM_SCALE"
elif [ "$QUALITY" = "high" ] ; then
# No rescaling
SCALE_OPT=""
else
echo "Error! Quality setting unknown \"$QUALITY\"." >&2
exit 1
fi
# Convert the pictures
while [ $# -gt 0 ] ; do
ALBUM="$1"
shift
ALBUM_NAME=$(basename "$ALBUM")
mkdir -p "$ALBUM_DEST/$ALBUM_NAME"
for FILE in "$ALBUM"/*.tiff ; do
DEST_NAME=$(basename "$FILE" | sed "s/tiff$/$FORMAT/")
DEST_FILE="$ALBUM_DEST/$ALBUM_NAME/$DEST_NAME"
echo "\"$FILE\" -> \"$DEST_FILE\""
# Should we rotate the image?
ORIENTATION=$(tiffinfo "$FILE" | sed -n 's/^[\t ]*Orientation: //p')
if [ "$ORIENTATION" = "row 0 top, col 0 lhs" ] ; then
ROTATE_OPT="" # No rotation
elif [ "$ORIENTATION" = "row 0 bottom, col 0 rhs" ] ; then
ROTATE_OPT="-rotate 180"
else
# Print a warning to tell the user the script should be
# improved :-)
echo "Warning! Image orientation unknown: \"$ORIENTATION\"." >&2
fi
# shellcheck disable=SC2086
convert $SCALE_OPT $ROTATE_OPT "$FILE" "$DEST_FILE"
done
done
echo
echo "Converted everything successfuly!"