How To Bulk Remove White Or Any Color Solid Background From Image

Bulk Remove Solid Background ImageMagick

In this blog post I will show you how to, in bulk, remove any color background to from any image.

Pre-requisites

  1. ImageMagick
  2. All of your images in one folder

How To Easily Install ImageMagick for Mac OSX

  1. First we install HomeBrew:
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

     

  2. Then we install ImageMagick:
    brew install ImageMagick

     

  3. Test that ImageMagick is installed by running the command magick, which should give you output with usage instructions

Removing Backgrounds From Images In Bulk

Once we have ImageMagick installed, we can begin to remove solid background colors like magic!

Here’s the command we will be working with:

find . -type f -name "*.jpg" -print0 | while IFS= read -r -d $'\0' file; do convert -verbose "$file" -transparent white "$file.png"; done
# Credit: http://www.vectorns.com/blog/technical-articles/18-how-to-remove-white-background-from-a-picture

To break this command down…

  1. First we are looking for all of the files with the extension .jpg. If you files have a different extension, just change that to whatever you need.
  2. Then we loop through all of the photos and convert white background to transparent
  3. At the same time of the loop, we convert the jpg file format to png (which support transparency)

The values in bold in my breakdown are the values that we can change. For example, let’s say we want to convert a black background to blue, we would run the following command:

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

If you find that there are some remnants of the white background you may need to add fuzz to the command, so it will like like:

convert /Users/mike/Downloads/photo\ work/transparent/your.png  -fuzz 20% -transparent white result.png

20% fuzz is a good place to start. Below are some example of how fuzz changes the output.

ImageMagick Avocado - Original ImageMagick Avocado - No Fuzz

Original vs. Transparent Background (No Fuzz)

ImageMagick Avocado - 20% Fuzz ImageMagick Avocado - 40% Fuzz

20% Fuzz vs 40% Fuzz

Mike Doubintchik

Author Mike Doubintchik

More posts by Mike Doubintchik

Leave a Reply