Colorize Image with PHP ImageMagick – keeping white pixels white

375 Views Asked by At

enter image description here

I would like to colorize an image with ImageMagick like shown in the image. But when I do it with

$image_color1 = new Imagick('file.png');
$image_color1->setImageType(Imagick::IMGTYPE_GRAYSCALEMATTE);
$color1 = new \ImagickPixel("rgb(0, 0, 220)");
$image_color1->colorizeImage($color1, 1, true);

the white pixels turn yellow like shown in the image.

When I do it with the PHP imagefilter like this

imagefilter($image_color1, IMG_FILTER_COLORIZE, 0, 0, 220)

it looks much better without different colors. So my question is: How can I receive a colorized image like shown above with PHP and ImageMagick (white needs to be white again).

OR: Can I use the PHP "IMG_FILTER_COLORIZE" in combination with ImageMagic? How would this work?

1

There are 1 best solutions below

0
fmw42 On

You can do the PHP equivalent of the following Imagemagick command line, which creates a LUT and applies it to the grayscale image.

Input:

enter image description here

convert img.png -colorspace gray \( -size 1x1 xc:blue xc:white +append -resize 256x1 \) -clut x.png

enter image description here

or

convert img.png -negate \( +clone -fill blue -colorize 100 -negate \) -compose multiply -composite -negate x.png

enter image description here