[graphics] Add tiff-batch-convert.sh

This commit is contained in:
Matteo Cypriani 2017-09-22 23:30:51 -04:00
parent a7c8880230
commit 934fdcc3e9
3 ha cambiato i file con 141 aggiunte e 0 eliminazioni

1
bin/tiff-batch-convert Link Simbolico
Vedi File

@ -0,0 +1 @@
../graphics/tiff-batch-convert.sh

Vedi File

@ -12,3 +12,47 @@ EXIF information is manipulated using exiftool, shipped with the
Image is rotated using ImageMagick's `convert`.
**Warning**: no backup of the original file is made!
tiff-batch-convert.sh
===============
`tiff-batch-convert.sh` converts TIFF images to another format of your choice,
such as JPEG.
Use case: you have a bunch of albums that you scanned to TIFF to preserve
quality, with a good resolution, each album being in its own directory. But
TIFFs are big; you want to generate compressed versions of the albums, possibly
down-scalling the images, to share on the Internet or copy them to your
smartphone.
Usage:
tiff-batch-convert.sh <DESTINATION> <ALBUM1> [ALBUM2 [...]]
`<DESTINATION>` is the base directory in which the converted albums will be
stored; each album will be in a separate sub-folder
For example, this will convert every album under
`$HOME/Pictures/my_albums_tiff` and store the result under
`/mnt/usb0/my_albums_lowres` (supposedly a USB drive):
tiff-batch-convert.sh /mnt/usb0/my_albums_lowres $HOME/Pictures/my_albums_tiff/*
Limitations and bugs:
- Existing destination files will be **OVERWRITEN WITHOUT WARNING**!
- Input files must have the extention `.tiff` (lowercase), not `.tif`.
- The albums provided on the command-line must contain TIFF images directly;
sub-folders won't be searched.
- Similarly, the full hierarchy of the input albums is *not* preserved: each
album will be generated as a subfolder of `<DESTINATION>`, even if the
original albums are stored in different places. It is easy to modify this
behaviour in the script if needed.
- To change the quality settings or the output format, you will have to edit
the script (see the `USER CONFIGURATION` section of the script).
Dependencies:
- `convert` from ImageMagick (pkgsrc: ImageMagick, Debian: imagemagick)
- `tiffinfo` from libtiff (pkgsrc: tiff, Debian: libtiff-tools)

96
graphics/tiff-batch-convert.sh Executable file
Vedi File

@ -0,0 +1,96 @@
#!/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"
# Resolution of the converted pictures: low, medium or high.
# Use "high" for no rescaling. The actual size of the pictures for "low" and
# "medium" depends on the next two options below.
QUALITY="medium"
# 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 #
#############################
# 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
# Do we have at least an input album and a destination folder?
if [ $# -lt 2 ] ; then
echo "Usage:" >&2
echo " $0 <DESTINATION> <ALBUM1> [ALBUM2 [...]]" >&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
ALBUM_DEST="$1"
shift
# 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
convert $SCALE_OPT $ROTATE_OPT "$FILE" "$DEST_FILE"
done
done
echo
echo "Converted everything successfuly!"