1
votes

I have about 2700 images that I want to:

  • Convert to .png
  • Make the white background, transparent

To do this, I downloaded ImageMagick using Homebrew and ran the below command in the relevant directory:

find . -type f -name "*.jpg" -print0 | while IFS= read -r -d $'\0' file; do convert -verbose "$file" -transparent white "$file.png"; done

This worked, however the images still have a few white specks around them as per the below image. With off-white bottles, it's even harder because it makes some of the bottle transparent too!

In photoshop, you can adjust the "tolerance" of "MagicWand" to ensure that this doesn't happen but I'm not sure how you can do this using ImageMagick and can't find anything on Google.

Example of Image with white crust around outside

Can anyone help? Is there a way of doing this with ImageMagick? Is there a better way of processing these 2700 images to remove the white background?

Thanks A

1
You could try allowing some "fuzz"... convert bottle.png -fuzz 10% -transparent white result.png - Mark Setchell
In practice you want to use the equivalent of Gimp's Color-to-alpha, that transforms the grey pixels on the edges onto partially transparent pixels. There is no such operator in magick but a good approximation exists, see here - xenoid

1 Answers

1
votes

Use -fuzz option in ImageMagick

$ convert img.jpg -fuzz 32% -transparent #ffffff out.png

This will allow you to adjust the tolerance value. Hope this helped.