JonTargaryen
JonTargaryen

Reputation: 1367

remove white background with ImageMagick but not the white inside picture?

im trying to delete the background with a batch process in pictures of products for an e-commerce site.

The problem is that the script also remove the white color in the inside of the product, leaving the product transparent in some areas..

For example:

enter image description here

Command:

convert *.jpg  -set filename: %t -fuzz 5% -transparent white %[filename:].png

this is the best I can get..im ok with this result around the product, but I need that the white inside the product remains white and not transparent.

enter image description here

Upvotes: 5

Views: 2425

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207670

The problem with your current approach is that it doesn't respect boundaries, it is just applied globally, making all white pixels transparent regardless of their connectivity to the background.

Instead, you will get on better using a "floodfill" that only floods into areas that are within the fuzz distance of the top-left corner pixel.

So, I chose an unused colour of magenta so you can see what is happening:

convert product.jpg -fuzz 5% -fill magenta -draw 'color 0,0 floodfill' result.png

enter image description here

You would then follow that with the command to make magenta transparent like this:

convert product.jpg -fuzz 5% -fill magenta -draw 'color 0,0 floodfill' -transparent magenta result.png

enter image description here

Upvotes: 2

Related Questions