-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimg_bg_rm
executable file
·101 lines (90 loc) · 3.35 KB
/
img_bg_rm
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
#!/usr/bin/env bash
set -euo pipefail
#
# clear black from an afni anatomical underlay image (-simple)
# or any background (top left pixel color) from any image
#
# will overwrite output image
#
_img_bg_rm(){
if [ $# -lt 2 ]; then
cat >&2 <<HEREDOC
USAGE: $0 input.png output.png [-simple] [-white2red] [-bg=black] [-fuzz=8] [-palette]; use '-x' as output to autoname"
$0 input.ppm -x # makes input_alpha.png
$0 input.ppm out.png # makes out.png
$0 mask.png -x -palette # smaller file size w/pngnq to reduce color pallete
$0 input.png -x -simple # usues 8% fuzz, does not remove inner black
$0 mask.png -x -simple -white2red # changes white to red
$0 mask.png -x -simple -bg=white # make white the alpha color
HEREDOC
return 1
fi
[ ! -r "$1" ] &&
echo "input image $1 does not exist" >&2 && return 1
# if you want to use the inputname as output
local in="$1"
local out="$2"
# use for creating out if '-x' and for warning message
local in_name=$(basename $in) # remove path
in_name=${in_name%.*} # remove ext
if [ "$2" == "-x" ]; then
out=$(dirname $in)/${in_name}_alpha.png
fi
extraCmd=""
simpleFuzz=8
#change white to red
if [ $# -ge 3 ] && [[ "$@" =~ -white2red ]]; then
simpleFuzz=13
extraCmd="-fuzz 100% -fill red -opaque white"
fi
# set color
bgcolor="black"
if [ $# -ge 3 ] && [[ "$@" =~ -bg=([^ ]+) ]]; then
bgcolor=${BASH_REMATCH[1]}
fi
# fuzz, default to 8, 13 if white2red
if [ $# -ge 3 ] && [[ "$@" =~ -fuzz=([^ ]+) ]]; then
simpleFuzz=${BASH_REMATCH[1]}
fi
#echo "settings: fuzz=$simpleFuzz extra=$extraCmd bg=$bgcolor"
# use simple and low fuzz (BTC derived 8% for afni exported images)
# or complicated w/ nice smoothing and no within-shell alpha
if [ $# -ge 3 ] && [[ "$@" =~ -simple ]]; then
convert ${in} \
-fuzz $simpleFuzz% -transparent $bgcolor \
$extraCmd \
${out}
else
# https://stackoverflow.com/questions/9155377/set-transparent-background-using-imagemagick-and-commandline-prompt
local color=$( convert "$in" -format "%[pixel:p{0,0}]" info:- )
[ -z "$color" ] && echo "$in top pixel is no color!" >&2 && return 1
# check size of input
if ! identify "$in" | awk -F'[ x]' '($3*$4 > 1000^2){exit(1)}'; then
echo "WARNING: image is kind of big. convert may take a while!" >&2
identify $in |sed 's/^/ /' >&2
echo "consider: " >&2
echo " $(basename $0) $in $out -simple -bg=$color -fuzz=30" >&2
echo " OR " >&2
echo " convert '$in' -scale 50% ${in_name}_small.png" >&2
echo " $(basename $0) ${in_name}_small.png ${@:2}" >&2
echo >&2
fi
set -x
convert "$in"\
-alpha off -bordercolor $color -border 1 \
\( +clone -fuzz 30% -fill none -floodfill +0+0 $color \
-alpha extract -geometry 200% -blur 0x0.5 \
-morphology erode square:1 -geometry 50% \) \
-compose CopyOpacity -composite -shave 1 \
$extraCmd \
"$out"
set +x
fi
if [ $# -ge 3 ] && [[ "$@" =~ -palette ]]; then
! [[ $out =~ (.*).png$ ]] && echo "palette only works for png!" >&2 && return
pngnq $out
mv ${BASH_REMATCH[1]}-nq8.png $out
fi
}
# run if script name is bg_rm
[ "$(basename $0)" == "img_bg_rm" ] && _img_bg_rm $@