-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_it
executable file
·570 lines (529 loc) · 22.4 KB
/
frame_it
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#!/bin/zsh
#
# author: walter rowe <walter@walterrowe.com>
# copyright 2022 walter rowe
#
####
# usage: PROGRAM [ OPTIONS ] image [ .. ]
#
# OPTIONS:
#
# -h | --help display a usage message with option descriptions
#
# -o | --overwrite specifies to overwrite the original files vs the default
# of keeping them and writing new files with "-framed" inserted
# before the suffix
#
# -s=suffix | suffix to append to decorated file names (default: "-framed")
# --suffix=suffix
#
# -t | --text specifies to use a two-line text logo underneath the image
#
# -ot | --overlaytext specifies to use a two-line text logo inside the image
#
# -l | --logo specifies to place an image-based logo underneath the image
# DEFAULT style when no options are specified
#
# -ol | --overlaylogo specifies to place an image-based logo inside the image
#
# -f | --frame add a picture frame around the sides of the image
# this disables bevel and enables shadowboxing (default no frame)
#
# -b | --bevel add an interior bevel between the image and surrounding matte.
# this disables shadowboxing and picture frame. (default no bevel)
#
# -p | --picture add a wall picture frame outer border for that wall mounted look.
# this disables shadowboxing and thin frame. (default no bevel)
#
# -d | --dropshadow specifies a drop shadow (offset bottom and right) vs
# the default that centers the image in an all-edges shadow
#
# -bc=color | specifies the color of the interior bevel (default #888888)
# --bevelcolor=color
#
# -fc=color | specifies the color of the inner picture frame (default white)
# --framecolor=color
#
# -mc=color | specifies the color of the matte background (default white)
# --mattecolor=color
#
# -pc=color | specifies the color of the wall picture frame (default #0a0a0a)
# --picturecolor=color
#
# -sc=color | specifies the color of the shadow (default black)
# --shadowcolor=color
#
# -tc=color | specifies the color of the two-line text logo
# --textcolor=color default #383838 below image, #E0E0E0 inside image
#
# -tf=font | specify the font to use for the two-row label text logo
# --textfont=font
#
# -a=ratio | specify the desired aspect ratio of the final output file
# --aspect=ratio in x:y ratio format (eg. -a=3:2, -a=1:1). some social media
# sites prefer specific aspect ratios for best display.
#
# -w=your_logo | specifies the image-based logo to use as the watermark that
# --watermark=your_logo that is placed below or inside the image.
#
# -g=placement | specifies which inside edge or corner of the image the logo
# --gravity=placement or text should be nearest. The following are accepted values.
# The logo or text is inset from the specified edge or corner.
# DEFAULT edge is south/bottom.
#
# +----------------------------------------------------+
# | topleft top topright |
# | |
# | left middle right |
# | |
# | bottomleft bottom bottomright |
# +----------------------------------------------------+
#
# +----------------------------------------------------+
# | northwest(nw) north northeast(ne) |
# | |
# | west center east |
# | |
# | southwest(sw) south southeast(se) |
# +----------------------------------------------------+
###
# text option - two text label lines
label1="W A L T E R R O W E P H O T O G R A P H Y"
label2="w w w . w a l t e r r o w e . c o m"
# default logo watermark file - graphic watermark (my logo watermark is 900wx250h pixels w/ transparent background)
watermark=$HOME/Pictures/watermarks/watermark_dark.png
# homebrew on the Apple ARM platform installs apps in /opt/homebrew/bin
if [ -x /opt/homebrew/bin/brew ]; then
eval $(/opt/homebrew/bin/brew shellenv)
fi
# homebrew on the Apple Intel platform installs apps in /usr/local/bin
if [ -x /usr/local/bin/brew ]; then
eval $(/usr/local/bin/brew shellenv)
fi
# ADD IMAGEMAGICK BINARY PATH HERE and uncomment
# export PATH=/path/to/your/imagemagick:$PATH
# usage
usage()
{
echo ""
if [ $# -gt 0 ]; then
echo error: $@
else
echo ""
echo "command: ${program_base} ${program_args}"
echo ""
sed >&2 -e "1,/^####/d; /^###/g; /^#/!q; s/^#//; s/PROGRAM/${program_base}/; s/^ //; 4,\$p" "$program_full"
fi
}
# allow specifying logo placement by common terms or imagemagick terms
# e.g. a person can specify --bottom or --south and it is translated to -gravity south
declare -A logo_gravity_opts=(
[topleft]=northwest
[top]=north
[topright]=northeast
[left]=west
[middle]=center
[right]=east
[bottomleft]=southwest
[bottom]=south
[bottomright]=southeast
[nw]=northwest
[ne]=northeast
[sw]=southwest
[se]=southeast
)
# make an egrep expression for the keys and values
logo_gravity_keys=$(echo ${(k)logo_gravity_opts} | sed -e 's; ;|;g')
logo_gravity_vals=$(echo ${(v)logo_gravity_opts} | sed -e 's; ;|;g')
# get name of script
eval program_base=$(basename $0)
eval program_full=$(which $0)
program_args=($@)
# must provide at least one file
if [ $# -lt 1 ]; then
usage must provide at least one image file name
exit 1
fi
# defaults
overwrite='false'
suffix='framed'
style='logo'
frame='false'
framecolor='white'
bevel='0x0'
bevelpad='0'
bevelcolor='#888888'
bevel_frame='0x0'
picture='false'
picturebevelinner='0x0'
picturebevelouter='0x0'
picturesurface='0x0'
picturecolor='#0a0a0a'
dropshadow='false'
shadowcolor='black'
inner='0'
outer='0'
logo_gravity='south'
mattecolor='white'
mattewidth='0x0'
textcolor='#383838'
overlaytextcolor='#E0E0E0'
textfont='Copperplate'
aspect='false'
aspectratio='0' # zero means no change to aspect ratio
# process command line options
for opt in "$@"; do
case ${opt} in
-h|--help)
usage
exit 0
;;
-o|--overwrite)
# do not overwrite the original files
overwrite='true'
shift
;;
-s=*|--suffix=*)
opt=$(echo ${opt} | cut -d= -f2)
suffix="${opt}"
shift
;;
-t|--text)
style='text'
shift
;;
-ot|--overlaytext)
style='overlay_text'
shift
;;
-l|--logo)
style='logo'
shift
;;
-ol|--overlaylogo)
style='overlay_logo'
shift
;;
-f|--frame)
inner="1"
outer="10"
bevel="0x0"
bevelpad="0"
frame='true'
shift
;;
-b|--bevel)
inner="0"
outer="0"
bevel="18x18+0+16"
bevelpad="18"
frame='false'
shift
;;
-p|--picture)
picture='true'
shift
;;
-d|--dropshadow)
dropshadow='true'
shift
;;
-bc=*|--bevelcolor=*)
opt=$(echo ${opt} | cut -d= -f2)
bevelcolor="${opt}"
shift
;;
-fc=*|--framecolor=*)
opt=$(echo ${opt} | cut -d= -f2)
framecolor="${opt}"
shift
;;
-mc=*|--mattecolor=*)
opt=$(echo ${opt} | cut -d= -f2)
mattecolor="${opt}"
shift
;;
-pc=*|--picturecolor=*)
opt=$(echo ${opt} | cut -d= -f2)
picturecolor="${opt}"
shift
;;
-sc=*|--shadowcolor=*)
opt=$(echo ${opt} | cut -d= -f2)
shadowcolor="${opt}"
shift
;;
-tc=*|--textcolor=*)
opt=$(echo ${opt} | cut -d= -f2)
textcolor="${opt}"
overlaytextcolor="${opt}"
shift
;;
-tf=*|--textfont=*)
opt=$(echo ${opt} | cut -d= -f2)
textfont="${opt}"
shift
;;
-a=*|--aspect=*)
opt=$(echo ${opt} | cut -d= -f2)
aspectratio="${opt}"
aspect=true
shift
;;
-w=*|--watermark=*)
opt=$(echo ${opt} | cut -d= -f2)
# evaluate watermark=your_logo to expand special chars like ~
eval watermark=${opt}
# make sure watermark really exists
if [ ! -f ${watermark} ]; then
usage watermark file \'${watermark}\' not found
exit 1
fi
shift
;;
-g=*|--gravity=*)
opt=$(echo ${opt} | cut -d= -f2)
# validate gravity location
echo ${opt} | egrep -wq "${logo_gravity_keys}|${logo_gravity_vals}"
if [ $? -ne 0 ]; then
usage invalid gravity placement \'${opt}\' specified
exit 1
fi
echo ${opt} | egrep -wq "${logo_gravity_keys}"
if [ $? -eq 0 ]; then
# gravity specified as top, bottomleft, etc
logo_gravity="${logo_gravity_opts[${opt}]}"
else
# gravity specified as north, southwest, etc
logo_gravity=${opt}
fi
shift
;;
-*)
usage invalid option \'${opt}\' specified
exit 1
;;
esac
done
# must provide at least one file
if [ $# -lt 1 ]; then
usage must provide at least one image file name
exit 1
fi
# cycle through the list of provided images
for filename in "$@" ; do
# skip over source files that do not exist
if [ ! -f "${filename}" ]; then
echo "File does not exist: '${filename}'"
continue
fi
# source file parts - use eval to ensure expansion of ~, et al
eval filepath=$(dirname "${filename:q}")
eval filetype=$(basename "${filename:q}" | awk -F. '{ print $NF }')
eval filebase=$(basename "${filename:q}" .${filetype})
# if mattecolor is transparent, set file type to png vs source file type
if [ $mattecolor = transparent ]; then
filetype=png
fi
# source file
eval source="${filename:q}"
# target file
if [ ${overwrite} = true ]; then
eval target="${source:q}"
else
eval target="${filepath:q}/${filebase:q}-${suffix}.${filetype}"
fi
# extract image file width and height
file_wide=$(identify -format %w "${source}")
file_high=$(identify -format %h "${source}")
# defaults based on 3000wx2400h source and 900wx250h logo
# - no dropshadow w/ 100% opacity and 25-pixel feather
# - pad left/top edges w/ 10 pixels
# - center so equal shadow on all edges
# convert 5x4 aspect ratio scaling to file's aspect ratio
if [ ${file_wide} -gt ${file_high} ]; then
scale_wide=$(expr 3000 \* ${file_wide} / ${file_high})
scale_high=$(expr 2400 \* ${file_wide} / ${file_high})
fi
if [ ${file_high} -gt ${file_wide} ]; then
scale_wide=$(expr 2400 \* ${file_high} / ${file_wide})
scale_high=$(expr 3000 \* ${file_high} / ${file_wide})
fi
if [ ${file_high} -eq ${file_wide} ]; then
scale_wide=$(expr 3000 \* ${file_high} / ${file_wide})
scale_high=$(expr 3000 \* ${file_high} / ${file_wide})
fi
# scale label (60% width of image looks good)
label_width=$(expr $file_wide \* 3 / 5)
# scale logo for other images sizes
logo_wide=$(identify -format %w "${watermark}")
logo_high=$(identify -format %h "${watermark}")
scaled_logo_wide=$(expr ${logo_wide} \* ${file_wide} / ${scale_wide})
scaled_logo_high=$(expr ${logo_high} \* ${file_high} / ${scale_high})
# scale padding, borders, shadow, logo placement
if [ ${dropshadow} = false ]; then
# settings for equal shadow around all edges of the image
padding="11x11"
shaded_area=$(expr 25 \* ${file_wide} / ${scale_wide})
shadow="100x${shaded_area}"
offset="+0+0"
offset_border="0x0"
scaled_logo_lift=$(expr 30 \* ${file_high} / ${scale_high})
# half of delta (30) between reference padded_high/wide w/ and w/o drop shadow (10 vs 40)
label_shadow_shift="10"
else
# settings for bottom right drop shadow below the image
padding="0x0"
# shift drop shadow layer down and right by 25 pixels, extend left/top matte to equalize matte width
shaded_area=$(expr 25 \* ${file_wide} / ${scale_wide})
border_area=$(expr 2 \* ${shaded_area})
shadow="100x${shaded_area}"
offset="+${shaded_area}+${shaded_area}"
offset_border="${border_area}x${border_area}"
scaled_logo_lift=$(expr 30 \* ${file_high} / ${scale_high})
label_shadow_shift="30"
fi
# if adding bevel, don't add shadowbox or right border matte padding
if [ $bevel != '0x0' ]; then
bevel_frame="${shaded_area}x${shaded_area}"
padding="0x0"
shadow="0x0"
offset="+0+0"
offset_border="0x0"
fi
# picture frame settings
if [ $picture = true ]; then
# turn off shadowboxing and print frame
padding="0x0"
bevel_frame="0x0"
shadow="0x0"
offset="+0+0"
offset_border="0x0"
inner="0"
outer="0"
frame='false'
# calculate scaled value for inside bevel
scaled_innerbevel1=$(expr 18 \* ${file_wide} / ${scale_wide})
scaled_innerbevel2=$(expr ${scaled_innerbevel1} - 2)
# calculate scaled value for wall frame bevel
scaled_picturebev1=$(expr 8 \* ${file_wide} / ${scale_wide})
scaled_picturebev2=$(expr ${scaled_picturebev1} - 2)
# calculate scaled value for wall frame surface
scaled_picturesurf=$(expr 75 \* ${file_wide} / ${scale_wide})
# calculate scaled value for matte width between image bevel and wall frame
scaled_mattewidth=$(expr 300 \* ${file_wide} / ${scale_wide})
# set scaled inner bevel
bevel="${scaled_innerbevel1}x${scaled_innerbevel1}+0+${scaled_innerbevel2}"
bevelpad=${scaled_innerbevel1}
# set scaled matte width
mattewidth="${scaled_mattewidth}x${scaled_mattewidth}"
# set wall frame inner bevel - note +0+${scaled_picturebev2}
picturebevelinner="${scaled_picturebev1}x${scaled_picturebev1}+0+${scaled_picturebev2}"
# set wall frame outer bevel - note +${scaled_picturebev2}+0
picturebevelouter="${scaled_picturebev1}x${scaled_picturebev1}+${scaled_picturebev2}+0"
# set wall frame top surface width
picturesurface="${scaled_picturesurf}x${scaled_picturesurf}"
fi
# inset overlay logos from the nearest edge based on gravity
if echo ${style} | egrep -q 'overlay'; then
case ${logo_gravity} in
center)
logo_inset="+0+0"
;;
north|south)
logo_inset="+0+${scaled_logo_lift}"
;;
west|east)
logo_inset="+${scaled_logo_lift}+0"
;;
northwest|northeast|southwest|southeast)
logo_inset="+${scaled_logo_lift}+${scaled_logo_lift}"
;;
esac
fi
# decorate the image file
case ${style} in
text)
# text above and below need horizontal padding when placed at west/east corners
label_padding=0
if echo ${logo_gravity} | egrep -q 'east|west' && test ${picture} = false ; then
label_padding=$(expr 20 + ${inner} + ${outer} + ${shaded_area} + ${label_shadow_shift})
fi
if echo ${logo_gravity} | egrep -q 'east|west' && test ${picture} = true ; then
label_padding=$(expr 20 + ${inner} + ${outer} + ${shaded_area} + ${label_shadow_shift} - ${scaled_innerbevel1})
fi
# use "+swap" to swap layer order before layer merge when placing text above the image
if echo ${logo_gravity} | egrep -q 'north' ; then
swap="+swap"
else
swap=""
fi
# convert source file into decorated target file
magick \
"${source}" -auto-orient \
-mattecolor "${bevelcolor}" -frame ${bevel} \
-mattecolor black -frame ${inner}x${inner} -mattecolor ${framecolor} -frame ${outer}x${outer} -mattecolor black -frame 1x1 -mattecolor ${mattecolor} -frame ${bevelpad}x${bevelpad} \
\( +clone -background ${shadowcolor} -shadow ${shadow}${offset} \) \
+swap -background ${mattecolor} -layers merge +repage \
-gravity northwest -background ${mattecolor} -splice ${offset_border} \
-mattecolor ${mattecolor} -frame ${padding} \
\( -background ${mattecolor} -fill "${textcolor}" -font "${textfont}" -size ${label_width}x -gravity ${logo_gravity} label:"${label1}\n${label2}" \
-append \) \
${swap} -append -layers merge +repage \
-bordercolor ${mattecolor} -border 20x20 \
-mattecolor ${mattecolor} -frame ${bevel_frame} \
-background ${mattecolor} -gravity center -extent "${aspectratio}#" \
-mattecolor ${mattecolor} -frame ${mattewidth} -mattecolor '#6b6b6b' -frame ${picturebevelinner} -mattecolor ${picturecolor} -frame ${picturesurface} -mattecolor '#333333' -frame ${picturebevelouter} \
-format ${filetype} "${target}"
;;
overlay_text)
# convert source file into decorated target file
magick "${source}" -auto-orient \
-background transparent -fill "${overlaytextcolor}" -font "${textfont}" -size ${label_width} -gravity ${logo_gravity} label:"${label1}\n${label2}" -geometry ${logo_inset} -composite \
-mattecolor "${bevelcolor}" -frame ${bevel} \
-mattecolor black -frame ${inner}x${inner} -mattecolor ${framecolor} -frame ${outer}x${outer} -mattecolor black -frame 1x1 -mattecolor ${mattecolor} -frame ${bevelpad}x${bevelpad} \
\( +clone -background ${shadowcolor} -shadow ${shadow}${offset} \) \
+swap -background ${mattecolor} -layers merge +repage \
-gravity northwest -background ${mattecolor} -splice ${offset_border} \
-mattecolor ${mattecolor} -frame ${padding} \
-bordercolor ${mattecolor} -border 20x20 \
-mattecolor ${mattecolor} -frame ${bevel_frame} \
-background ${mattecolor} -gravity center -extent "${aspectratio}#" \
-mattecolor ${mattecolor} -frame ${mattewidth} -mattecolor '#6b6b6b' -frame ${picturebevelinner} -mattecolor ${picturecolor} -frame ${picturesurface} -mattecolor '#333333' -frame ${picturebevelouter} \
-format ${filetype} "${target}"
;;
logo)
# convert source file into decorated target file
magick "${source}" -auto-orient \
-mattecolor "${bevelcolor}" -frame ${bevel} \
-mattecolor black -frame ${inner}x${inner} -mattecolor ${framecolor} -frame ${outer}x${outer} -mattecolor black -frame 1x1 -mattecolor ${mattecolor} -frame ${bevelpad}x${bevelpad} \
\( +clone -background ${shadowcolor} -shadow ${shadow}${offset} \) \
+swap -background ${mattecolor} -layers merge +repage \
-gravity northwest -background ${mattecolor} -splice ${offset_border} \
-gravity ${logo_gravity} -background ${mattecolor} -splice 0x${scaled_logo_high} \
\( ${watermark} -resize ${scaled_logo_wide}x${scaled_logo_high}\> -gravity ${logo_gravity} -geometry +0+${scaled_logo_lift} \) -composite \
-mattecolor ${mattecolor} -frame ${padding} \
-mattecolor ${mattecolor} -frame 20x20 \
-mattecolor ${mattecolor} -frame ${bevel_frame} \
-background ${mattecolor} -gravity center -extent "${aspectratio}#" \
-mattecolor ${mattecolor} -frame ${mattewidth} -mattecolor '#6b6b6b' -frame ${picturebevelinner} -mattecolor ${picturecolor} -frame ${picturesurface} -mattecolor '#333333' -frame ${picturebevelouter} \
-format ${filetype} "${target}"
;;
overlay_logo)
# convert source file into decorated target file
magick "${source}" -auto-orient \
\( ${watermark} -resize ${scaled_logo_wide}x${scaled_logo_high}\> -gravity ${logo_gravity} -geometry ${logo_inset} \) -composite \
-mattecolor "${bevelcolor}" -frame ${bevel} \
-mattecolor black -frame ${inner}x${inner} -mattecolor ${framecolor} -frame ${outer}x${outer} -mattecolor black -frame 1x1 -mattecolor ${mattecolor} -frame ${bevelpad}x${bevelpad} \
\( +clone -background ${shadowcolor} -shadow ${shadow}${offset} \) \
+swap -background ${mattecolor} -layers merge +repage \
-gravity northwest -background ${mattecolor} -splice ${offset_border} \
-mattecolor ${mattecolor} -frame ${padding} \
-mattecolor ${mattecolor} -frame 20x20 \
-mattecolor ${mattecolor} -frame ${bevel_frame} \
-background ${mattecolor} -gravity center -extent "${aspectratio}#" \
-mattecolor ${mattecolor} -frame ${mattewidth} -mattecolor '#6b6b6b' -frame ${picturebevelinner} -mattecolor ${picturecolor} -frame ${picturesurface} -mattecolor '#333333' -frame ${picturebevelouter} \
-format ${filetype} "${target}"
;;
esac
done