-
Notifications
You must be signed in to change notification settings - Fork 5
/
mk-background-transparent.sh
36 lines (26 loc) · 1.26 KB
/
mk-background-transparent.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
#!/bin/bash
# for making an image background transparent...
# I used this script for preparing a transparent background signature image.
# written by Ye Kyaw Thu, LST, NECTEC, Thailand
# last updated: 13 May 2021
# Reference Link:
# https://legacy.imagemagick.org/discourse-server/viewtopic.php?t=32090
# https://legacy.imagemagick.org/discourse-server/viewtopic.php?f=1&t=36208
# https://legacy.imagemagick.org/discourse-server/viewtopic.php?t=18196
# https://stackoverflow.com/questions/28136471/image-enhancement-solution
# https://en.wikipedia.org/wiki/SRGB
# if you want to keep foreground colour, following one command is OK
# convert ./akm-sign.png -transparent white bg-transparent-fg-color.png
imageFILE=$1;
# convert to gray scale image
convert $imageFILE -colorspace Gray -auto-level ./gray.png;
# get background color information
bgColor=`convert ./gray.png -format "%[pixel:u.p{0,0}]" info:`;
# fill background color with white color
convert ./gray.png -fuzz 50% -fill 'srgb(255,255,255)' -opaque $bgColor ./gray-white.png;
# make transparent background
convert ./gray-white.png -transparent 'srgb(255,255,255)' -fuzz 30% ./bg-transparent.png;
# check the output
display $imageFILE & display ./bg-transparent.png;
# clean temp files
rm ./gray.png ./gray-white.png;