background to transparent but leave the white clothing as solid white

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Post Reply
eddyparkinson
Posts: 2
Joined: 2017-02-06T00:07:50-07:00
Authentication code: 1151

background to transparent but leave the white clothing as solid white

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
eddyparkinson
Posts: 2
Joined: 2017-02-06T00:07:50-07:00
Authentication code: 1151

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

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply