Making background White

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
JagVoylla
Posts: 17
Joined: 2014-08-01T11:34:57-07:00
Authentication code: 6789

Making background White

Post by JagVoylla »

All,

I have a image with has more of Golden hues . I wanted to change the background to white. Tried this , and background became all black

convert IM8.jpg -alpha set -channel RGBA -fuzz 1% -fill none -floodfill +0+0 white IM8_Flood.jpg

What am I missing?


Thanks
JagVoylla
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Making background White

Post by fmw42 »

convert IM8.jpg -alpha set -channel RGBA -fuzz 1% -fill none -floodfill +0+0 white IM8_Flood.jpg
jpg does not support transparency, and your command is trying to fill any white with transparency.

You probably want

convert IM8.jpg -alpha set -channel RGBA -fuzz 1% -fill white -floodfill +0+0 backgroundcolor IM8_Flood.jpg

But you should use the exact value of your background color so that -fuzz XX% can use as small a value as possible. I do not recommend saving as jpg, since it is a compressed format and may change your background color of white to something only close to white.

What is your IM version and platform.

try this instead as a simpler command. It finds the color at 0,0 automatically

Code: Select all

convert IM8.jpg -fuzz 1% -fill white -draw "color 0,0 floodfill" IM8_Flood.jpg
If that does not work, try increasing the fuzz value or post a link to your image. You can upload to any free image hosting service, such as dropbox.com (public folder).
JagVoylla
Posts: 17
Joined: 2014-08-01T11:34:57-07:00
Authentication code: 6789

Re: Making background White

Post by JagVoylla »

Thank you so much for your help. Really appreciate this

I have uploaded the change image here :https://dl.dropboxusercontent.com/u/793 ... lood_2.jpg
This is the image I started with :https://dl.dropboxusercontent.com/u/79303486/I2.jpg

I see that the circle in the middle is still not cleaned up, Is there a way to clean up this as well.

I also want to remove the thread on top of the image , is there a way to do so?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Making background White

Post by fmw42 »

Background removal is a very hard process when the background is not constant, especially with shadows.

I think you have reached the limits of what you can do easily with background removal. Shadows are very hard to deal with. When the background is shaded, sometimes you can make a background image from the outer parts of the image and scale it to the full image size. See how snibgo and I tried to deal with that at viewtopic.php?f=1&t=25991

But here the shadow is in the center and I don't see a way to deal with that. Also there is no way to remove the thread, besides some kind of manual painting with the background color or outlining manually to create a mask (same for the inside shadow).

There are some tools that try to do automated background remove or user guided. See

https://clippingmagic.com/
http://docs.opencv.org/trunk/doc/py_tut ... abcut.html

However, IM does not currently have anything like that.

Perhaps one of the other IM users might have another approach that will help further.

Also see http://www.imagemagick.org/Usage/masking/#bg_remove
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Making background White

Post by fmw42 »

What was the exact command you used. One may be able to clean it up some by replacing the background with "none" transparency so that one can use the alpha channel to do some other mask processing.
JagVoylla
Posts: 17
Joined: 2014-08-01T11:34:57-07:00
Authentication code: 6789

Re: Making background White

Post by JagVoylla »

I started with JPG and used following sequence :-
convert IMTEST.jpg -level 3.921%,96.08%,0.95 IM6.jpg
convert IM6.jpg -brightness-contrast 3 IM7.jpg
convert IM7.jpg -fuzz 1% -fill white -draw "color 0,0 floodfill" IM8_Flood.jpg

As you suggested in earlier replies , it might be hard to have transparency related commands on jpg , I can start with a open file as well. We are shooting these pictures so I can get any required format.

Thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Making background White

Post by fmw42 »

I would caution against saving to jpg for intermediate images. You will lose quality at each step due the lossy compression.

You can do all your commands in one single command.

Code: Select all

convert IMTEST.jpg -level 3.921%,96.08%,0.95 -brightness-contrast 3 -fuzz 1% -fill white -draw "color 0,0 floodfill" IM8_Flood.jpg
This command does not work on the image you provided earlier I2.jpg

So this is not the command you used for that image.
JagVoylla
Posts: 17
Joined: 2014-08-01T11:34:57-07:00
Authentication code: 6789

Re: Making background White

Post by JagVoylla »

Hi,

Sorry, I put in wrong link , here is the original image :https://dl.dropboxusercontent.com/u/79303486/IMTEST.jpg

When I tried combined command, I saw the white fill was not consistent. Please see the image here for https://dl.dropboxusercontent.com/u/793 ... lood_3.jpg for result of the combined command.

Thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Making background White

Post by fmw42 »

Same image as before. The different results are because you are saving intermediate images as jpg.

If you use one command the intermediate images are not compressed.

But neither your commands or the one single one are making the whole outside completely white, though the single command does a better job. The reason is that the background is not a constant color. So you need to increase the fuzz value.

Try the single command with -fuzz 10%. Now all the outside is white, but the inside is still unchanged. You need to use floodfill in the inside ring as as well, but that will still leave some non-white.

To see, try

Code: Select all

convert IMTEST.jpg -level 3.921%,96.08%,0.95 -brightness-contrast 3 -fuzz 10% -fill white -draw "color 0,0 floodfill color 540,1000 floodfill" IM8_Flood_new.jpg
You could try doing the floodfill in the inside in more places to try to clear up more of the background.
JagVoylla
Posts: 17
Joined: 2014-08-01T11:34:57-07:00
Authentication code: 6789

Re: Making background White

Post by JagVoylla »

Hi,

Thanks for all your help. Could finish quite a bit of it. Image which have pearls in them are new problem that I am struggling with . The fill white command write on pearls as well. Any help in this will be greatly appreciated.
https://dl.dropboxusercontent.com/u/793 ... G_7367.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Making background White

Post by fmw42 »

your background is too similar in color to the pearls. this is very hard to work with. try the following, but it is not perfect and some of the pearls are gone. Here I try to recreate the background image from the 4 corners of the image, interpolating to fill it out. Then I do -compose divide to remove the background.

Code: Select all

convert IMG_7367.png \
\( -clone 0 -gravity northwest -crop 400x400+0+0 +repage \) \
\( -clone 0 -gravity northeast -crop 400x400+0+0 +repage \) \
\( -clone 0 -gravity southwest -crop 400x400+0+0 +repage \) \
\( -clone 0 -gravity southeast -crop 400x400+0+0 +repage \) \
\( -clone 1 -clone 2 +append \) \
\( -clone 3 -clone 4 +append \) \
-delete 1-4 \
\( -clone 1 -clone 2 -append -scale 1936x3000! \) \
-delete 1,2 \
+swap -compose divide -composite result.png
Post Reply