-
Notifications
You must be signed in to change notification settings - Fork 1
/
watermark.sh
executable file
·206 lines (178 loc) · 6.61 KB
/
watermark.sh
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#! /bin/bash
## constants ##
WATERMARK=/home/kakurady/works/2017/kakurady_lettering_watermark.png
#CUSTOM_QTABLE=/home/kakurady/dev/watermark_images/qtbl_Peterson93.txt # optional
CJPEG=/home/kakurady/apps/mozjpeg/bin/cjpeg
COMPOSITE=composite
CONVERT=convert
DSSIM=/home/kakurady/Downloads/dssim/bin/dssim
EXIFTOOL=exiftool
EARGFILES=/usr/share/doc/libimage-exiftool-perl/arg_files/
IPTC2PNGARGS=/home/kakurady/dev/watermark_images/iptc2png.args
PARALLEL=parallel
CWEBP=cwebp
parallel_params="--bar --ungroup --load 95%"
USAGE_CMDLINE="Usage: $0 [-gwks] files ..."
USAGE="$USAGE_CMDLINE
-g set gravity
-w skip watermarking
-k keep original file
-s report DSSIM
"
## variables ##
keep_original=""
report_dssim=""
gravity="southeast"
do_watermark="y"
while getopts g:wks option_name
do
case $option_name in
g) gravity=$OPTARG;;
k) keep_original="y";;
s) report_dssim="y";;
w) do_watermark="";;
\?) echo "$USAGE"; exit 1;;
esac
done
shift $(($OPTIND - 1))
if [ ! -f "$WATERMARK" ]
then
echo "watermark image ($WATERMARK) not found, exiting."
exit 1
fi
if [ $# -lt 1 ]
then
echo "$USAGE"
exit 1
fi
if [ ! -d watermarked ]
then
mkdir watermarked
fi
if [ ! -d webp ]
then
mkdir webp
fi
if [ ! -d resized ]
then
mkdir resized
fi
if [ -f "$CUSTOM_QTABLE" ]
then
if [ ! -d resized_92_p93 ]
then
mkdir resized_92_p93
fi
if [ ! -d resized_92_p87 ]
then
mkdir resized_92_p87
fi
fi
report_ssim() {
local filename
filename=${1%.*}
$DSSIM $@ > "$filename.dssim.txt"
shift
wc -c $@ > "$filename.wc.txt"
cut -f 1 "$filename.dssim.txt" | paste - "$filename.wc.txt" | tee "$filename.report.txt"
rm "$filename.dssim.txt" "$filename.wc.txt"
}
# Remember to export variables for parallel
doIt() {
local watermarked_image
echo "working on $1"
#convert full-sized image
#
# this is an "archival" quality image suitable for printing
# saved at an arbitrary 95 quality factor
#
# Using PNG as intermediate format to preserve color space info
# PNG "quality" 14 means zlib compress 1 + Paeth filtering
$CONVERT -quality 14 "$1" "${1%.*}.temp.png"
$EXIFTOOL -use MWG -charset iptc=UTF8 -tagsFromFile "$1" -icc_profile -charset iptc=UTF8 -tagsFromFile "$1" -exif:serialnumber= -exif:lensserialnumber= -MakerNotes:all= -overwrite_original -@ "$EARGFILES/exif2xmp.args" -@ "$EARGFILES/iptc2xmp.args" -@ "$IPTC2PNGARGS" "-XMP-dc:Title<IPTC:Healine" "$1" "${1%.*}.temp.png"
$CJPEG -quant-table 2 -quality 95 -fastcrush -outfile "${1%.*}.jpg" "${1%.*}.temp.png"
#composite watermarked image
#
# this image is uploaded to Flickr / Weasyl, which will scale it down
# also suitable for display on a 2x1080p / UHD display
# JPEG quality factor 70 here is arbitrary
#
# since the display is a lot denser you can get away with throwing away details ("compressive image"). (however, you can't compare quality factors between encoders with different quantization tables; comparing subjective quality at a given file size is more reasonable [JPEG files don't actually have a quality factor; you're scaling a 64-entry table on how accurately details are stored in a 8x8 block])
if [ "$do_watermark" ]
then
watermarked_image="watermarked/${1%.*}_watermarked.png"
$COMPOSITE -gravity $gravity -geometry +4+4 -quality 3 "$WATERMARK" "${1%.*}.temp.png" "$watermarked_image"
rm "${1%.*}.temp.png"
else
watermarked_image="${1%.*}.temp.png"
fi
$CJPEG -quality 70 -quant-table 2 -outfile "watermarked/${1%.*}_large.jpg" "$watermarked_image"
# shrink down watermarked image
#
# this image is specifically for Fur Affinity (and some other chat apps
# that will refuse dimensions > 4096)
#
# quality factor 92.5 here.
# 85 was too low, problems on e.g. Buster Bunny's fur
# 95 would use as much as 40% more data than 92.5 and 100% more than 85
# 91.25 would probably suffice (saves 5% over 92.5)
# but I can't tell differences after looking at too many photos
# quant table 2 was personal pref; no discernable difference from
# table 0 at the same quality factor
$CONVERT "$watermarked_image" -gamma .45455 -resize 1200x960 -gamma 2.2 -quality 3 "watermarked/${1%.*}_resized.png"
rm "$watermarked_image"
$CJPEG -quant-table 2 -quality 92.5 -outfile "watermarked/${1%.*}_medium.jpg" "watermarked/${1%.*}_resized.png"
#test subsampling modes
#$CJPEG -quant-table 2 -quality 87 -sample 1x1 -outfile "resized_87_1x1/${1%.*}_1.jpg" "resized/${1%.*}_resized.png"
#$CJPEG -quant-table 2 -quality 92.5 -sample 2x2,1x1,2x2 -outfile "resized_92_212/${1%.*}_2.jpg" "resized/${1%.*}_resized.png"
if [ -f "$CUSTOM_QTABLE" ]
then
$CJPEG -quality 90 -qtables "$CUSTOM_QTABLE" -qslots 0,1,2 -sample 1x1 -outfile "resized_92_p93/${1%.*}_p.jpg" "watermarked/${1%.*}_resized.png"
$CJPEG -quality 87 -qtables "$CUSTOM_QTABLE" -qslots 0,1,2 -sample 1x1 -outfile "resized_92_p87/${1%.*}_q.jpg" "watermarked/${1%.*}_resized.png"
fi
#convert "resized/${1%.*}_resized.png" "resized/${1%.*}_resized.png"
if [ "$report_dssim" -a -f "$DSSIM" ]
then
report_ssim "watermarked/${1%.*}_resized.png" "watermarked/${1%.*}_medium.jpg" "resized_87_1x1/${1%.*}_1.jpg" "resized_92_212/${1%.*}_2.jpg" "resized_92_p93/${1%.*}_p.jpg" "resized_92_p87/${1%.*}_q.jpg"
fi
rm "watermarked/${1%.*}_resized.png" #"resized/${1%.*}_resized.png"
#add exif tags
#
#May need to add color space if not sRGB. However, that bloats image (by 6kb)
#if only we could insert the PNG built-in sRGB chunk from RawTherapee?
$EXIFTOOL -ignoreMinorErrors -use MWG -charset iptc=UTF8 -tagsFromFile "$1" -icc_profile -all -exif:serialnumber= -exif:lensserialnumber= -MakerNotes:all= -overwrite_original "${1%.*}.jpg" "watermarked/${1%.*}_medium.jpg" "watermarked/${1%.*}_large.jpg"
# "resized_87_1x1/${1%.*}_1.jpg" "resized_92_212/${1%.*}_2.jpg" "resized_92_p93/${1%.*}_p.jpg" "resized_92_p87/${1%.*}_q.jpg"
# $CWEBP -lossless -z 6 -metadata all -o "webp/${1%.*}.webp" -- "$1"
$CWEBP -near_lossless 80 -z 4 -metadata all -o "webp/${1%.*}_80.webp" -- "$1"
#move RawTherapee sidecar file, if one exist
if [ -f "$1.out.pp3" ]
then
mv "$1.out.pp3" "${1%.*}.jpg.out.pp3"
fi
# delete the original file
if [ ! "$keep_original" ]
then
rm "$1"
else
true
fi
}
#for f in *.tif; do doIt $f; done
export -f report_ssim
export -f doIt
export CJPEG
export COMPOSITE
export CONVERT
export DSSIM
export EXIFTOOL
export CUSTOM_QTABLE
export WATERMARK
export CWEBP
export keep_original
export gravity
export do_watermark
export EARGFILES
export IPTC2PNGARGS
$PARALLEL $parallel_params doIt ::: "$@"
# cat resized/*.report.txt >> resized/report_summary.txt
# rm resized/*.report.txt