change image background

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
kmanoj24
Posts: 1
Joined: 2018-09-23T05:37:58-07:00
Authentication code: 1152

change image background

Post by kmanoj24 »

Hi
I am using image magic command to convert the image background :

exec("convert test.JPG -resize 500x500 -quality 100 demo.png");
exec("convert demo.png \( +clone -fx 'p{10,10}' \) -compose Difference -composite -modulate 100,0 -alpha off difference.png");
exec("convert difference.png -bordercolor black -border 0 -threshold 15% -blur 0x3 -sharpen 0x10 halo_mask.png");
exec("convert demo.png -bordercolor white -border 0 halo_mask.png -alpha Off -compose CopyOpacity -composite test_halo.png");
demo.png
demo.png (346.05 KiB) Viewed 39298 times
converted image
test_halo.png
test_halo.png (310.89 KiB) Viewed 39297 times
converted image quality is not good and it also not make whole background white

Expected image :
test_halo.png
test_halo.png (310.89 KiB) Viewed 39297 times
Anybody please help me to found the expected solution.
Attachments
process.jpg
process.jpg (7.6 KiB) Viewed 39297 times
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: change image background

Post by fmw42 »

What is your Imagemagick version and platform, since syntax may vary.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: change image background

Post by fmw42 »

I have taken a different approach to work in HSL colorspace. Since your shoes are red and black would be similar in HSL, I rotate the colors by 180 degrees (-modulate 100,100,0) and separate the saturation channel (after reviewing all 3 channels for the best one to use). The color rotation was probably not needed since I am using saturation rather than hue. I then threshold the result and use -connected-components to remove noise and fill gaps. Then I use some morphology to smooth the result. The result is a black/white mask that I put into the alpha channel of the image.

Input:
Image

Unix Syntax:

Code: Select all

convert demo.png \
\( -clone 0 -modulate 100,100,0 -colorspace HSL \
-channel g -separate +channel -threshold 6% -type bilevel \
-define connected-components:area-threshold=150 \
-define connected-components:mean-color=true \
-connected-components 4 \
-morphology smooth octagon:2 \) \
-alpha off -compose copy_opacity -composite \
demo_nobg.png
Image


If you want a white background, then one way is

Code: Select all

convert demo.png \
\( -clone 0 -modulate 100,100,0 -colorspace HSL \
-channel g -separate +channel -threshold 6% -type bilevel \
-define connected-components:area-threshold=150 \
-define connected-components:mean-color=true \
-connected-components 4 \
-morphology smooth octagon:2 \) \
-alpha off -compose copy_opacity -composite \
-background white -alpha background -alpha off \
demo_nobg2.png
Image
Post Reply