Page 1 of 1

background to transparent but leave the white clothing as solid white

Posted: 2017-02-06T00:17:10-07:00
by eddyparkinson
First time posting

I have a few hundred images like this:
http://i.imgur.com/0Us8nwX.jpg
http://i.imgur.com/rfEfbAl.jpg
http://i.imgur.com/KtoEux9.jpg

I want to change only the white background to transparent, but leave the white clothing as solid white.
Any thoughts of where to start. Tools I could use etc.
I am a programmer. Am looking for a way to automate the image processing if possible.

I have installed imagemagick and looked a a few posts on background removal and edge detection.
This also shows how to use

Code: Select all

-transtparent
option https://www.youtube.com/watch?v=fMg6Y9sb4cY
but it is not a simple one color swap.

Re: background to transparent but leave the white clothing as solid white

Posted: 2017-02-06T00:42:50-07:00
by snibgo
Please always state your IM version and platform.

This works for your first image:

Code: Select all

convert whiteShirt.jpg -alpha set -bordercolor white -border 1 -fill none -fuzz 3% -draw "color 0,0 floodfill" -shave 1x1 d.png
What this does:

- Enable transparency
- Add white border 1 pixel wide
- Flood-fill making pixels transparent, starting flood from 0,0, where pixels are within 3% of white
- Remove the 1-pixel border. You might prefer "-trim".

Your input is JPG, so flat white areas are no longer flat white. If you have better inputs, you should use those.

For multiple files, just put the command in a for loop.

Re: background to transparent but leave the white clothing as solid white

Posted: 2017-02-06T18:51:18-07:00
by eddyparkinson
Very good. Thanks - worked on 17 out of 20. Not sure about images like http://i.imgur.com/rfEfbAl.jpg I tried
-canny and -edge but it misses too much of the edge.

Re: background to transparent but leave the white clothing as solid white

Posted: 2017-02-07T03:53:46-07:00
by snibgo
The shoulder at top-left has merged with the background.

I suppose someone has photographed each garment, and created a clipping path around it, or painted out the background.

Then, sadly, they have flattened the image against white and saved the image as a JPEG. You want to undo these steps.