Page 1 of 1
Removing complex white background + cropping
Posted: 2013-06-17T03:34:18-07:00
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
Re: Removing complex white background + cropping
Posted: 2013-06-17T05:14:11-07:00
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.
Re: Removing complex white background + cropping
Posted: 2013-06-17T07:31:16-07:00
by holdenk
thank you. Any idea on how to make it black ?
Re: Removing complex white background + cropping
Posted: 2013-06-17T08:02:42-07:00
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
Re: Removing complex white background + cropping
Posted: 2013-06-17T09:25:01-07:00
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 !!
Re: Removing complex white background + cropping
Posted: 2013-06-17T09:32:53-07:00
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.
Re: Removing complex white background + cropping
Posted: 2013-06-17T10:03:38-07:00
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.
Re: Removing complex white background + cropping
Posted: 2013-06-18T03:40:48-07:00
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...
Re: Removing complex white background + cropping
Posted: 2013-06-18T10:10:03-07:00
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.