Browse Source

[graphics] Add tiff-batch-convert.sh

Matteo Cypriani 1 year ago
parent
commit
934fdcc3e9
3 changed files with 141 additions and 0 deletions
  1. 1
    0
      bin/tiff-batch-convert
  2. 44
    0
      graphics/README.md
  3. 96
    0
      graphics/tiff-batch-convert.sh

+ 1
- 0
bin/tiff-batch-convert View File

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

+ 44
- 0
graphics/README.md View File

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

+ 96
- 0
graphics/tiff-batch-convert.sh View File

@@ -0,0 +1,96 @@
1
+#!/bin/sh
2
+#
3
+# tiff-batch-convert.sh, Copyright © 2017 Matteo Cypriani
4
+#
5
+# This program is free software. It comes without any warranty, to the extent
6
+# permitted by applicable law. You can redistribute it and/or modify it under
7
+# the terms of the Do What The Fuck You Want To Public License, Version 2, as
8
+# published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more
9
+# details.
10
+#
11
+# This script batch-converts TIFF images to another format of your choice.
12
+#
13
+# Dependencies:
14
+# - convert from ImageMagick
15
+# - tiffinfo from libtiff
16
+
17
+set -e
18
+#set -x
19
+
20
+######################
21
+# USER CONFIGURATION #
22
+######################
23
+
24
+# Format to convert the TIFF images to (lowercase)
25
+FORMAT="jpg"
26
+
27
+# Resolution of the converted pictures: low, medium or high.
28
+# Use "high" for no rescaling. The actual size of the pictures for "low" and
29
+# "medium" depends on the next two options below.
30
+QUALITY="medium"
31
+
32
+# Maximum size of the pictures for the "low" quality setting.
33
+LOW_SCALE="800x800"
34
+
35
+# Maximum size of the pictures for the "medium" quality setting.
36
+MEDIUM_SCALE="2000x2000"
37
+
38
+#############################
39
+# END OF USER CONFIGURATION #
40
+#############################
41
+
42
+# Should the pictures be resized, depending on the quality setting?
43
+if [ "$QUALITY" = "low" ] ; then
44
+	SCALE_OPT="-scale $LOW_SCALE"
45
+elif [ "$QUALITY" = "medium" ] ; then
46
+	SCALE_OPT="-scale $MEDIUM_SCALE"
47
+elif [ "$QUALITY" = "high" ] ; then
48
+	# No rescaling
49
+	SCALE_OPT=""
50
+else
51
+	echo "Error! Quality setting unknown \"$QUALITY\"." >&2
52
+	exit 1
53
+fi
54
+
55
+# Do we have at least an input album and a destination folder?
56
+if [ $# -lt 2 ] ; then
57
+	echo "Usage:" >&2
58
+	echo "	$0 <DESTINATION> <ALBUM1> [ALBUM2 [...]]" >&2
59
+	echo "DESTINATION is the base directory in which the converted albums will be stored." >&2
60
+	echo "Each ALBUM is a directory containing TIFF files." >&2
61
+	exit 1
62
+fi
63
+
64
+ALBUM_DEST="$1"
65
+shift
66
+
67
+# Convert the pictures
68
+while [ $# -gt 0 ] ; do
69
+	ALBUM="$1"
70
+	shift
71
+	ALBUM_NAME=$(basename "$ALBUM")
72
+	mkdir -p "$ALBUM_DEST/$ALBUM_NAME"
73
+
74
+	for FILE in "$ALBUM"/*.tiff ; do
75
+		DEST_NAME=$(basename "$FILE" | sed "s/tiff$/$FORMAT/")
76
+		DEST_FILE="$ALBUM_DEST/$ALBUM_NAME/$DEST_NAME"
77
+		echo "\"$FILE\" -> \"$DEST_FILE\""
78
+
79
+		# Should we rotate the image?
80
+		ORIENTATION=$(tiffinfo "$FILE" | sed -n 's/^[\t ]*Orientation: //p')
81
+		if [ "$ORIENTATION" = "row 0 top, col 0 lhs" ] ; then
82
+			ROTATE_OPT="" # No rotation
83
+		elif [ "$ORIENTATION" = "row 0 bottom, col 0 rhs" ] ; then
84
+			ROTATE_OPT="-rotate 180"
85
+		else
86
+			# Print a warning to tell the user the script should be
87
+			# improved :-)
88
+			echo "Warning! Image orientation unknown: \"$ORIENTATION\"." >&2
89
+		fi
90
+
91
+		convert $SCALE_OPT $ROTATE_OPT "$FILE" "$DEST_FILE"
92
+	done
93
+done
94
+
95
+echo
96
+echo "Converted everything successfuly!"

Loading…
Cancel
Save