Search code examples
imagemagickimagemagick-convert

Colorize Specific Regions of an Image Using a Mask with ImageMagick


I'm trying to colorize specific regions of an image based on a mask using ImageMagick. The mask contains white pixels where the image should be colorized and black pixels where no colorization should occur.

The command I've tried:

magick convert input_image.png mask.png -alpha off -compose CopyOpacity -composite -fill "#FF5733" -colorize 100% output_image.png

Problem:

  • the entire image is getting colorized instead of only the white areas defined by the mask.

System Info:

  • OS: Windows 10
  • ImageMagick version: 7.1.0-27

Solution

  • Here is one way to do that in Imagemagick. Create a colorized image the size of the input. Then use the mask to composite them together.

    (Don't use magick convert --- use just magick)

    Image:

    enter image description here

    Mask:

    enter image description here

    magick lena.png ( +clone -fill blue -colorize 100 ) mask.png -compose over -composite lena_colorized.png
    

    enter image description here