-
Notifications
You must be signed in to change notification settings - Fork 1
/
stackprepare
executable file
·595 lines (554 loc) · 16.5 KB
/
stackprepare
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#! /bin/bash
# stackprepare - prepare image stack with ImageMagick
# Notes:
# add --meanbalance?
# add --align?
usage() {
echo "
stackprepare - prepare image stack
Usage:
stackprepare [OPTIONS] IMAGES ...
Options:
-f, --force Force to write into already existing folder.
-h, --help Show this help.
-o, --output DIR Store prepared images in directory DIR.
Default: ./prepared
-V, --showimage Show images in image viewer geeqie.
-1, --one Show one middle image only, do not store.
For the following options and their arguments see also documentation of
ImageMagick options with same name.
--compose [=MODE] Compose image with itself with mode MODE.
Possible MODE see: magick -list compose
Some modes of interest: Interpolate Overlay
SoftBurn SoftDodge SoftLight HardLight
Bumpmap Screen Multiply Freeze VividLight
Modulate ColorBurn. Default: Interpolate
--contrast Enhance contrast the simple way.
--contrast-stretch [=ARG] Contrast with color stretch. Default: auto
--crop [=GEOMETRY] Crop a part of the images. GEOMETRY is WxH+X+Y.
Use only once within a preparestack command.
Instead of a geometry you can specify a region
with TBLRM for top, bottom, left, right, middle,
e.g. --crop=TL for a square from the top left.
Default: 800x800 from the middle.
--gamma [=ARG] Adjust image gamma. Default: auto
--level [=ARG] Adjust image level. Default: auto
--resize [=ARG] Resize images. Default: 50%
--rotate [=DEGREE] Rotate images clockwise. Default: 90
--sharpen [=ARG] Sharpen images. Default: 2x1
--shave [=ARG] Shave edges from images. Default: 30%
--sigmoidal-contrast [=ARG] Enhance contrast. Default: 3x50%
--im OPTIONS ImageMagick options to apply literally.
Option sets:
--set [=SET] Apply a predefined set of options. SET can be:
gamma1 gamma2 gamma3
level1 level2 level3
stretch1 stretch2 stretch3
sharp1 sharp2 sharp3
Depends on:
- ImageMagick
- geeqie (optional)
"
}
### messages
showimage() {
[ "$Showimageprocessing" = "yes" ] && geeqie -t -r --File:"${1:-}" 2>/dev/null &
#viewnior ${1:-} &
return 0
}
error() {
echo "preparestack ERROR: ${1:-}" >&2
exit 1
}
### misc
digitonly() {
echo "${1//[^0-9.]/}"
}
printsameline() {
# print $1 without newline at begin of current line
echo -ne "${1:-}\033[0K\r" >&2
}
### run commands on all CPUs
multicore() {
# Run multiple processes in parallel, but not more than $Multicore_maxprocesses
# $1 Command
# $2 Image to show if $1 is finished
# Run multicore_wait afterwards to wait for the last processes to finish.
local Process Command
local Mem_needed Zram
[ "${1:-}" = "-t1" ] && {
shift
[ "$Multicore_processcount" -gt 0 ] && {
multicore_wait || return 1
}
}
[ "$Multicore_processcount" = "$Multicore_maxprocesses" ] && {
multicore_wait || return 1
}
[ "$Multicore_processcount" = "0" ] && Multicore_memorymax="$(printfreememory)"
Mem_needed=0
for Process in $(seq $Multicore_maxprocesses); do
Mem_needed="$(awk "BEGIN {print $Mem_needed + ${Multicore_memory[$Process]:-0} }" )"
done
Mem_needed="$((Mem_needed+${3:-0}))"
[ "$Mem_needed" -gt "$Multicore_memorymax" ] && {
note "multicore: Low memory. Waiting for $Multicore_processcount running processes to finish. Need: $((Mem_needed/1000)) MB, Available: $((Multicore_memorymax/1000)) MB"
[ "$Mem_needed" -gt "$Multicore_memorymax" ] && [ "$Multicore_processcount" = "0" ] && note "multicore: Likely hard disk cache will be used and slow down the calculation."
multicore_wait || return 1
}
ifcmdbreak && return 1
Multicore_processcount=$((Multicore_processcount +1))
Command="$(cut -d ' ' -f1 <<< "${1:-}")"
case $(type -t "$Command") in
file) Command="nice ${1:-}" ;;
*) Command="${1:-}" ;;
esac
#verbose "multicore: ${1:-}"
eval "$Command &"
Multicore_process[Multicore_processcount]=$!
Multicore_image[Multicore_processcount]="${2:-}"
Multicore_memory[Multicore_processcount]="${3:-0}"
return 0
}
multicore_wait() {
local Process Error=
for Process in $(seq ${Multicore_maxprocesses:-0}); do
[ "${Multicore_process[$Process]}" ] && {
multicore_waitprocess "${Multicore_process[$Process]}" || {
multicore_break
Error=1
}
[ "$Error" ] && break
[ "${Multicore_image[$Process]}" ] && showimage "${Multicore_image[$Process]}"
}
Multicore_process[$Process]=""
Multicore_image[$Process]=""
Multicore_memory[$Process]="0"
done
[ "$Error" ] && return 1
Multicore_processcount=0
return 0
}
multicore_waitprocess() {
local Error=
while sleep 0.2 ; do
ps -p "${1:-}" >/dev/null || break
ifcmdbreak && Error=1
[ "$Error" ] && break
done
[ "$Error" ] && return 1
wait "${1:-}"
return $?
}
multicore_break() {
local Process
for Process in $(seq ${Multicore_maxprocesses:-0}); do
[ "${Multicore_process[$Process]}" ] && {
verbose "multicore_break: Sending SIGINT to $(ps -p ${Multicore_process[$Process]})"
kill "${Multicore_process[$Process]}"
wait "${Multicore_process[$Process]}"
Multicore_process[$Process]=""
Multicore_image[$Process]=""
Multicore_memory[$Process]="0"
}
done
}
multicore_init() {
# declare global variables
local Process
Multicore_maxprocesses="$(nproc)"
Multicore_maxprocesses="${Multicore_maxprocesses:-1}"
Multicore_maxprocesses="$((Multicore_maxprocesses * 2))"
for Process in $(seq $Multicore_maxprocesses); do
Multicore_process[$Process]=""
Multicore_image[$Process]=""
Multicore_memory[$Process]="0"
done
Multicore_processcount=0
Multicore_minram=250000
Multicore_maxprocesses=$Multicore_maxprocesses
}
printfreememory() {
# print current free memory including zram
local Memory Line Zram
Memory="$(LC_ALL=C free | grep "Mem:" | LC_ALL=C awk '{print $7}')"
while read Line; do
Zram="$(LC_ALL=C awk 'BEGIN {OFMT = "%.0f"} {print ($3 - $4)}' <<< "$Line")"
Zram="$(LC_ALL=C awk 'BEGIN {OFMT = "%.0f"} {print $1 / 1000}' <<< "$Zram")"
#Memory="$((Memory + Zram))"
done < <(/sbin/swapon --bytes | grep zram)
echo $Memory
}
ifcmdbreak() {
#[ -e "$Cachedir/exit" ]
return 1
}
### image processing
meanproperties() {
Imageproperties="$($Magickbin $Sourceimagelist -evaluate-sequence mean -format '
Imageautogamma=%[fx:log(mean)/log(0.5)]
Imageminlevel=%[fx:minima*100]
Imagemaxlevel=%[fx:maxima*100]
Imagewidth=%[w]
Imageheight=%[h]
' -write info: \
-filter box -resize 1x1! -format "Imagemeancolor='%[pixel:u]'" -write info: \
-delete 0 \
-exit)"
eval $Imageproperties
}
check_imagesize() {
Imageproperties="$($Magickbin $(head -n1 <<< "$Sourceimagelist") -format '
Imagewidth=%[w]
Imageheight=%[h]
' -write info: \
-delete 0 \
-exit)"
eval $Imageproperties
}
prepare_focusstack() {
local Image Outputimage Command Count
Count=0
while read Image; do
Count="$((Count+1))"
Outputimage="$(rev <<< "$Image" | cut -d. -f2- | rev).tif"
Outputimage="$(basename "$Outputimage")"
Outputimage="$Destinationdir/$Outputimage"
printsameline "preparestack: Processing image $Count / $Imagenumber"
Command="
$Magickbin
$Image
$Prepareoptions
$Storeimage
-write $Outputimage
-delete 0
-exit"
#$Command
multicore "$(tr -d "\n" <<< "$Command")" "$Outputimage"
Error="$?"
[ "$Error" != "0" ] && break
#showimage "$Outputimage"
done <<< "$Sourceimagelist"
multicore_wait
echo ""
[ "$Oneimageonly" = "yes" ] && echo "$Command"
[ "$Error" != "0" ] && error "magick failed: $Command"
return 0
}
### main
declare_variables() {
# Global variables with default values
Cropgeometry=""
Destinationdir="./prepared"
Force="no"
Imageautogamma=""
Imageheight=""
Imagemaxlevel=""
Imagemeancolor=""
Imageminlevel=""
Imagenumber=""
Imagewidth=""
Magickbin=""
Oneimageonly=""
Prepareoptions=""
Showimageprocessing=""
Sourceimagelist=""
Storeimage="
-orient undefined
+repage
-compress lzw
-type TrueColor
-define png:compression-filter=2
-define png:compression-level=4
-define png:compression-strategy=1"
X=""
Y=""
W=""
H=""
return 0
}
parse_options() {
local Shortoptions Longoptions Parsedoptions Terminate= Arg=
Shortoptions="1fho:V"
Longoptions="force,help,one,output:,showimage"
Longoptions="$Longoptions,im:,set::"
Longoptions="$Longoptions,compose::,contrast,contrast-stretch::,crop::,gamma::,level::,resize::,rotate::,sharpen::,shave::,sigmoidal-contrast::"
Parsedoptions="$(getopt --options "$Shortoptions" --longoptions "$Longoptions" --name "$0" -- "$@")" || error "Option parsing error."
eval set -- "$Parsedoptions"
while [ $# -gt 0 ]; do
case "${1:-}" in
-h|--help)
usage
Terminate="yes"
break
;;
-1|--one)
Oneimageonly="yes"
;;
-o|--output)
Destinationdir="${2:-}"
shift
;;
-f|--force)
Force="yes"
;;
-V|--showimage)
Showimageprocessing="yes"
;;
### Imagemagick options
--compose)
Prepareoptions="$Prepareoptions
-write mpr:composeimage
mpr:composeimage
-compose ${2:-PegtopLight}
-composite
+set registry:composeimage"
shift
;;
--contrast)
Prepareoptions="$Prepareoptions
-contrast"
shift
;;
--contrast-stretch)
Arg="${2:-auto}"
[ "$Arg" = "auto" ] && Arg=""
Arg="${2:-"IMAGEMINLEVELxIMAGEMAXLEVEL"}"
Prepareoptions="$Prepareoptions
-contrast-stretch $Arg"
shift
;;
--crop)
Arg="${2:-M}"
Cropgeometry="${Arg:-}"
Prepareoptions="$Prepareoptions
-crop IMAGECROPGEOMETRY"
shift
;;
--gamma)
Arg="${2:-auto}"
[ "$Arg" = "auto" ] && Arg=""
Arg="${2:-"IMAGEAUTOGAMMA"}"
Prepareoptions="$Prepareoptions
-gamma $Arg"
shift
;;
--im)
Prepareoptions="$Prepareoptions
${2:-}"
shift
;;
--level)
Arg="${2:-auto}"
[ "$Arg" = "auto" ] && Arg=""
Arg="${2:-"IMAGEMINLEVEL%xIMAGEMAXLEVEL%"}"
Prepareoptions="$Prepareoptions
-level $Arg"
shift
;;
--resize)
Arg="${2:-"50%"}"
Prepareoptions="$Prepareoptions
-resize $Arg"
shift
;;
--rotate)
Arg="${2:-0}"
Prepareoptions="$Prepareoptions
-background transparent
-rotate ${2:-}"
shift
;;
--set)
Arg="${2:-"gamma1"}"
case "$Arg" in
gamma1)
Prepareoptions="$Prepareoptions
-gamma IMAGEAUTOGAMMA
-sigmoidal-contrast 3x50%
-sharpen 0x1"
;;
gamma2)
Prepareoptions="$Prepareoptions
-gamma IMAGEAUTOGAMMA
-sigmoidal-contrast 5x50%
-sharpen 0x2"
;;
gamma3)
Prepareoptions="$Prepareoptions
-gamma IMAGEAUTOGAMMA
-sigmoidal-contrast 7x50%
-sharpen 0x4"
;;
level1)
Prepareoptions="$Prepareoptions
-level IMAGEMINLEVEL%xIMAGEMAXLEVEL%
-sigmoidal-contrast 3x50%
-sharpen 0x1"
;;
level2)
Prepareoptions="$Prepareoptions
-level IMAGEMINLEVEL%xIMAGEMAXLEVEL%
-sigmoidal-contrast 5x50%
-sharpen 0x2"
;;
level3)
Prepareoptions="$Prepareoptions
-level IMAGEMINLEVEL%xIMAGEMAXLEVEL%
-sigmoidal-contrast 7x50%
-sharpen 0x4"
;;
stretch1)
Prepareoptions="$Prepareoptions
-sigmoidal-contrast 3x50%
-sharpen 0x1
-contrast-stretch IMAGEMINLEVELxIMAGEMAXLEVEL"
;;
stretch2)
Prepareoptions="$Prepareoptions
-sigmoidal-contrast 5x50%
-sharpen 0x2
-contrast-stretch IMAGEMINLEVELxIMAGEMAXLEVEL"
;;
stretch3)
Prepareoptions="$Prepareoptions
-sigmoidal-contrast 7x50%
-sharpen 0x4
-contrast-stretch IMAGEMINLEVELxIMAGEMAXLEVEL"
;;
sharp1)
Prepareoptions="$Prepareoptions
-contrast
-sigmoidal-contrast 3x50%
-sharpen 0x1"
;;
sharp2)
Prepareoptions="$Prepareoptions
-contrast
-sigmoidal-contrast 5x50%
-sharpen 0x2"
;;
sharp3)
Prepareoptions="$Prepareoptions
-contrast
-sigmoidal-contrast 7x50%
-sharpen 0x4"
;;
*)
echo "Option --set: Unknown set '$Arg'" >&2
Terminate="yes"
break
;;
esac
shift
;;
--sharpen)
Arg="${2:-"2x1"}"
grep -q "x" <<< "$Arg" || Arg="${Arg}x1"
Prepareoptions="$Prepareoptions
-sigmoidal-contrast $Arg"
shift
;;
--shave)
Arg="${2:-"30%"}"
Prepareoptions="$Prepareoptions
-shave $Arg
+repage"
shift
;;
--sigmoidal-contrast)
Arg="${2:-"3x50%"}"
grep -q "x" <<< "$Arg" || Arg="${Arg}x50%"
Prepareoptions="$Prepareoptions
-sigmoidal-contrast $Arg"
shift
;;
--) ;;
*)
Sourceimagelist="$Sourceimagelist
${1:-}"
[ -f "${1:-}" ] || error "File not found: ${1:-}"
;;
esac
shift
done
Sourceimagelist="$(grep . <<< "$Sourceimagelist")"
Sourceimagelist="$(sort -V <<< "$Sourceimagelist")"
Imagenumber="$(grep -c . <<< "$Sourceimagelist")"
[ "$Imagenumber" = "0" ] && error "No source images specified."
[ "$Terminate" ] && finish 1
return 0
}
finish() {
trap - SIGINT
trap - EXIT
trap - ERR
multicore_break
exit 0
}
main() {
set -Eu
trap finish SIGINT
trap finish EXIT
trap finish ERR
declare_variables
parse_options "$@"
multicore_init
# -1, --one
[ "$Oneimageonly" = "yes" ] && {
Sourceimagelist="$(head -n5 <<< "$Sourceimagelist" | tail -n1)"
Imagenumber="1"
Destinationdir="/tmp"
Showimageprocessing="yes"
}
# check dependencies
command -v convert >/dev/null && Magickbin="convert"
command -v magick >/dev/null && Magickbin="magick"
[ -z "$Magickbin" ] && error "Error: imagemagick not found."
# -V, --showimage
[ "$Showimageprocessing" = "yes" ] && {
command -v geeqie >/dev/null || error "-V, --showimage: geeqie not found."
geeqie -t -r --File:"$(head -n1 <<< "$Sourceimagelist")"
}
# get and insert some image stack properties if needed
grep -q -E 'IMAGE|CROPGEOMETRY' <<< "$Prepareoptions" && {
grep -q -E 'LEVEL|GAMMA' <<< "$Prepareoptions" && meanproperties
[ -z "$Imagewidth" ] && check_imagesize
# --crop
[ "$Imagewidth" -gt 640 ] && W="800" || W="$((Imagewidth/2))"
[ "$Imageheight" -gt 480 ] && H="800" || H="$((Imageheight/2))"
Cropgeometry="${Cropgeometry:-"M"}"
grep -q M <<< "$Cropgeometry" && X="$(( Imagewidth/2 -W/2 ))" && Y="$(( Imageheight/2 -H/2 ))"
grep -q T <<< "$Cropgeometry" && Y="$(( Imageheight/4 -H/2 ))"
grep -q B <<< "$Cropgeometry" && Y="$(( Imageheight*3/4 -H/2 ))"
grep -q L <<< "$Cropgeometry" && X="$(( Imagewidth/4 -W/2 ))"
grep -q R <<< "$Cropgeometry" && X="$(( Imagewidth*3/4 -W/2 ))"
Cropgeometry="$(tr -d "MTBLR" <<< "$Cropgeometry")"
[ -z "$Cropgeometry" ] && Cropgeometry="${W}x${H}+${X}+${Y}"
Prepareoptions="$(sed "s/IMAGEMINLEVEL/$Imageminlevel/g ; s/IMAGEMAXLEVEL/$Imagemaxlevel/g ; s/IMAGEAUTOGAMMA/$Imageautogamma/g ; s/IMAGECROPGEOMETRY/$Cropgeometry/g" <<< "$Prepareoptions")"
}
# --output
[ -e "$Destinationdir" ] && [ "$Destinationdir" != "/tmp" ] && case "$Force" in
"yes")
case "$Destinationdir" in
""|"$HOME"|"/")
error "--output: Invalid output directory: '$Destinationdir'"
;;
*)
#rm -r "$Destinationdir" || return 1
;;
esac
;;
"no")
error "Output directory already exists:
$Destinationdir
You can force to use it with option --force."
;;
esac
mkdir -p "$Destinationdir" || return 1
# run
prepare_focusstack || return 1
return 0
}
main "$@"
finish