Just Jeff

Convert Non-White Background with ImageMagick

# Description: This script converts the background of an input image to pure white.
# Usage: white_background.sh <input_image> <fuzz_percentage>
# Arguments:
#   - input_image: Path to the image file whose background is to be changed.
#   - fuzz_percentage: A percentage indicating the fuzziness level for color matching, 
#   used to identify and change similar but not exact colors.
# Requirements: ImageMagick must be installed to use the 'convert' command.
# Output: Generates a new image file with the same name as the input but with '_white' 
#         appended before the file extension. The new image has a pure white background.

For my purposes, I use this to convert white background (color) to transparent.

Just Jeff

    if [ "$#" -ne 2 ]; then
        echo "Usage: white_background <input_image> <fuzz_percentage>"
        return 1
    fi

    local input_image=$1
    local fuzz_percentage=$2
    local output_image="${input_image%.*}_white.${input_image##*.}"

    convert "$input_image" -fuzz "$fuzz_percentage" -transparent "#FFFFFF" -fill "#FFFFFF" -opaque "#FFFFFF" "$output_image"
    echo "Converted $input_image to $output_image with pure white background."
}

Behavior with -transparent "#FFFFFF"

This option is used to make all pixels of the color #FFFFFF (white) in the image transparent. It's a crucial step when you want to isolate non-white elements of the image or when you want to change the white background to a different color or transparency.

https://imagemagick.org

#imagemagick