3

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

Elfordy
  • 57
  • 1
  • 8
  • 1
    You could try allowing some *"fuzz"*... `convert bottle.png -fuzz 10% -transparent white result.png` – Mark Setchell Oct 13 '17 at 13:23
  • 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](https://stackoverflow.com/questions/26408022/imagemagick-color-to-alpha-like-the-gimp) – xenoid Oct 14 '17 at 12:03

1 Answers1

2

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.

nipunasudha
  • 2,427
  • 2
  • 21
  • 46
  • Thanks Nipunasudha. This did help! But also, for bottles that are similar to white, it's making some of the bottle transparent. Is there any way I can avoid this from happening? – Elfordy Oct 16 '17 at 08:15
  • Here is the problem, each photo is different from each other, and only you know what is the subject is. I don't think there is a clean way to do this, unless you train a machine learning model to detect the subject. I am a graphic designer too, and it's not easy to remove background from a bottle and make it transparent without losing details, even manually. – nipunasudha Oct 16 '17 at 08:29