Removing complex white background + cropping

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
holdenk
Posts: 4
Joined: 2013-06-17T02:00:54-07:00
Authentication code: 6789

Removing complex white background + cropping

Post by holdenk »

Hi all ImageMagick fans !

I am try to remove a white background from a picture (a basic one + one more difficult).

1st image : http://www.mediafire.com/?d0ecarvi82kd5x2

1) I have managed to get the difference http://www.mediafire.com/?wco4g2if4n0ize4 using this script :
convert in1.jpg ( +clone -fx "p{0,0}" ) -compose Difference -composite -modulate 100,0 -alpha off in1_diff.png

2) But I did not manage to get the mask (http://www.mediafire.com/?lvxgyt442mnowkd) :
convert in1_diff.png -threshold 0 in1_mask.png

3) If the mask is good, i can remove the background using this script :
convert in1.jpg in1_mask.png -alpha off -compose CopyOpacity -composite out1.png

can you help me to complete the 2nd step ? about the crop i think i can do it my self.


The 2nd image is http://www.mediafire.com/view/uyb0iycy7gqndtx/in2.jpg
im not sure the first scripts work, do you have any idea?

thank you so much !

Holden
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Removing complex white background + cropping

Post by snibgo »

If you examine in1_diff.png with Gimp's eyedropper, you will see that most of the "black" pixels aren't actually black. So your threshold is wrong.
snibgo's IM pages: im.snibgo.com
holdenk
Posts: 4
Joined: 2013-06-17T02:00:54-07:00
Authentication code: 6789

Re: Removing complex white background + cropping

Post by holdenk »

thank you. Any idea on how to make it black ?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Removing complex white background + cropping

Post by snibgo »

If you are trying to turn the background transparent, it's easier in a single step:

Code: Select all

convert in1.jpg -fuzz 20%% -transparent White in1t.png
snibgo's IM pages: im.snibgo.com
holdenk
Posts: 4
Joined: 2013-06-17T02:00:54-07:00
Authentication code: 6789

Re: Removing complex white background + cropping

Post by holdenk »

wow that's great...but the problem with this code is that if i have white inside my picture, it will go transparent as well ?
For example, take the 2nd picture, I really got no idea how to manage this...

thanks a lot !!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Removing complex white background + cropping

Post by snibgo »

Yes, that simple command will turn all near-white pixels transparent. See http://www.imagemagick.org/Usage/masking/#bg_remove (and the other related matter on that page) for more sophisticated methods.
snibgo's IM pages: im.snibgo.com
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Removing complex white background + cropping

Post by GreenKoopa »

convert in1.jpg -fill none -fuzz 10% -floodfill +0+0 white out1.png
convert in2.jpg -fill none -fuzz 40% -floodfill +0+0 white out2.png

Your original idea of creating a mask and copying it in wasn't wrong. That method could help with more complex needs. You could achieve better transition than this binary alpha I showed. You could keep the shadow as semi-transparent. snibgo's link to the documentation is the place to start.
holdenk
Posts: 4
Joined: 2013-06-17T02:00:54-07:00
Authentication code: 6789

Re: Removing complex white background + cropping

Post by holdenk »

thanks everyone,
in fact, i already start with the tutorial http://www.imagemagick.org/Usage/masking/#bg_remove
the point i could not do is to create the mask as described in the tutorial :

convert difference.png -threshold 0 boolean_mask.png
convert cyclops.png boolean_mask.png \
-alpha off -compose CopyOpacity -composite \
cyclops_boolean.png


maybe i missed something...
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Removing complex white background + cropping

Post by GreenKoopa »

Now I can see that your first post is right out of that Usage Page suggested. As snibgo first responded, and the Cyclops example from that page shows, your threshold is too low because your background isn't perfect (which is common, especially for a jpeg source). Try:

convert in1_diff.png -threshold 10%% in1_mask.png

This method may not work for your second image with it's internal white, so I offered an alternative. Using -floodfill and your original masking technique can be combine if you have a reason to.
Post Reply