-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphoto-wall-convert
executable file
·176 lines (153 loc) · 6.16 KB
/
photo-wall-convert
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash
global_author=''
event=''
output_dir=converted
crop=false
open_dir=false
quiet=false
font_color=white
font_shadow_color=black
gravity=west
counter_total=0
counter_converted=0
resize=false
font_size=112
text_margin=150
text_shadow_deviation=3
font='/usr/share/fonts/redhat/RedHatDisplay-Bold.ttf'
usage() {
echo "Usage: $(basename "$0") --event <event_title> [--quiet] [--crop] [--resize] [--font-color <white|black>] [--gravity <east|west>] [--author <author>] <photo> [ <photo> ...]"
echo
echo "Description:"
echo "A simple tool to label and convert photographs for the big wall screen."
echo "It serves primarily for adding an annotation with author and event name to the photo, and it also sets Copyright exif data."
echo "The output is stored into the $output_dir directory."
echo
echo "Arguments:"
echo " --event <event_title> is a short string"
echo " --author <author> (optional) name of the person who took the photo that also holds the copyright; if omitted, it is taken from exif data"
echo " --crop Crop the image to the expected ratio"
echo " --font-color <white|black> Font color can be either white or black, white is the default"
echo " --gravity <west|east> Whether the annotation should be on the left (west) or right (east), west is the default"
echo " --resize Resize the photo to 7680x4820. Otherwise the size will not be affected, but the text size will be adjusted to appear the same"
echo " --open Open the output directory after conversion is done"
echo " --quiet Do not write verbose output"
}
perror() {
echo "$*" >&2
}
pinfo_indent() {
pinfo " $*"
}
pinfo() {
[ "${quiet}" == true ] && return
echo "$*"
}
while [ $# -ge 1 ] ; do
case $1 in
--help|-h) usage ; exit 0 ;;
--author) global_author=${2:-} ; shift 2 ;;
--event) event=${2:-} ; shift 2 ;;
--font-color) font_color=${2:-} ; shift 2 ;;
--gravity) gravity=${2:-} ; shift 2 ;;
--resize) resize=true ; shift ;;
--crop) crop=true ; shift ;;
--open) open_dir=true ; shift ;;
--quiet) quiet=true ; shift ;;
*) break ;;
esac
done
if ! command -v convert >/dev/null || ! command -v identify >/dev/null ; then
echo "Commands 'convert' or 'identify' not found. Install them by running 'yum install ImageMagick'"
exit 1
fi
if ! command -v exiftool >/dev/null >/dev/null ; then
echo "Command 'exiftool' not found. Install it by running 'yum install perl-Image-ExifTool'"
exit 1
fi
if ! command -v bc >/dev/null ; then
echo "Command 'bc' not found. Install it by running 'yum install bc'"
exit 1
fi
if ! [ -f "$font" ] ; then
echo "Font $font not found. Install it by running 'yum install redhat-display-fonts'"
exit 1
fi
if [ -z "$event" ] ; then
echo "Event not set."
usage
exit 1
fi
if [ $# -eq 0 ] ; then
echo "No input files given."
usage
exit 1
fi
if [ "$font_color" = black ] ; then
font_color=black
font_shadow_color=white
elif [ "$font_color" != white ] ; then
echo "Font color is allowed to be either white or black."
usage
exit 1
fi
[ -d "${output_dir}" ] || mkdir "${output_dir}"
tmpfile=$(mktemp)
while read -r f ; do
o="converted/$f"
pinfo "Processing $f to $o ..."
((counter_total++))
if [ -z "$global_author" ] ; then
author=$(exiftool "$f" | grep '^Creator ' | sed -e 's/.*: //')
if [ -z "$author" ] ; then
perror "Error: Author not found (Creator exif field not set) in $f. Set the author by --author option. Ignoring this file."
continue
fi
else
author=$global_author
fi
pinfo_indent "Author: $author"
cp "$f" "${tmpfile}"
width=$(identify -format "%w" "$f")
height=$(identify -format "%h" "$f")
((height_should_be=width*9/16))
if [ "$height" -ge $(("$height_should_be"+2)) ] || [ "$height" -le $(("$height_should_be"-2)) ] ; then
pinfo_indent "For width $width the height should be $height_should_be, but it is $height."
if [ "$crop" == true ] ; then
if [ "$height" -gt "$height_should_be" ] ; then
((crop_px=(height-height_should_be)/2))
mogrify -crop "${width}x${height_should_be}+0+${crop_px}" "${tmpfile}"
pinfo_indent "Cropped from ${width}x${height} to ${width}x${height_should_be}+0+${crop_px}"
else
((width_should_be=height*16/9))
((crop_px=(width-width_should_be)/2))
mogrify -crop "${width_should_be}x${height}+${crop_px}+0" "${tmpfile}"
pinfo_indent "Cropped from ${width}x${height} to ${width_should_be}x${height}+${crop_px}+0"
fi
else
perror "Error: Fix the ratio first or use --crop option. Ignoring this file."
continue
fi
fi
pinfo_indent "Setting exif data (author and copyright)..."
exiftool -overwrite_original -Copyright="$author" -Author="$author" -q "${tmpfile}"
pinfo_indent "Converting and annotating..."
if [ "$resize" = true ] ; then
pinfo_indent "Resizing the photo from ${width}x${height} to 7680x4320..."
convert "${tmpfile}" -resize "7680x4320" -font "$font" -fill "$font_shadow_color" -gravity "south${gravity}" -pointsize "$font_size" -annotate +"$text_margin"+"$text_margin" "$event\n$author" -append -fill "$font_color" -pointsize "$font_size" -annotate +$(($text_margin-$text_shadow_deviation))+$(($text_margin+$text_shadow_deviation)) "$event\n$author" "$o"
else
ratio=$(echo "scale=3; $width / 7680" | bc)
font_size=$(echo "scale=3; $font_size * $ratio" | bc)
text_margin=$(echo "scale=3; $text_margin * $ratio" | bc)
text_shadow_deviation=$(echo "scale=3; $text_shadow_deviation * $ratio" | bc)
pinfo_indent "Photo dimensions are ${width}x${height}, adjusting the text by ${ratio} (text size ${font_size})..."
convert "${tmpfile}" -font "$font" -fill "$font_shadow_color" -gravity "south${gravity}" -pointsize "$font_size" -annotate +"$text_margin"+"$text_margin" "$event\n$author" -append -fill "$font_color" -pointsize "$font_size" -annotate +$(echo "scale=3; $text_margin - $text_shadow_deviation" | bc)+$(echo "scale=3; $text_margin + $text_shadow_deviation" | bc) "$event\n$author" "$o"
fi
pinfo "Done ($o)"
((counter_converted++))
done <<< "$(ls "$@")"
rm -f "${tmpfile}"
if [ "$open_dir" == true ] ; then
xdg-open "${output_dir}"
fi
pinfo "Converted $counter_converted from $counter_total"