Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making white background transparent using ImageMagick

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

like image 752
Elfordy Avatar asked Sep 19 '25 18:09

Elfordy


1 Answers

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.

like image 105
nipunasudha Avatar answered Sep 21 '25 12:09

nipunasudha