Change Color of image using Imagick

Hi all!

So I have an image that has various shades of grey (I converted it to grayscale). And what I would like to allow the user to do is convert the image to a different color by selecting from a color picker (applying a hex code basically).


$shading = new Imagick($this->shading);
$shading->colorizeImage("#5780bd",1);
$this->blank->compositeImage($shading, Imagick::COMPOSITE_DEFAULT, 0,0);

Where $this->shading is the grayscale image and $this->blank is an image with just a white background.

The code does change the color of the grayscale image; but it doesn’t do it 100% correctly. For example, if I open photoshop and use the color code #5780bd and change the shading image to that color by using the pencil tool or w/e, the image color is noticeably different.

If anyone has any advice on how to accomplish coloring a gray-scale image to a hex code then that would be great. I’m not sure whether the original image should be grayscale or if it should be black or white or anything in order to achieve correct colorization.

I guess no one knows :frowning:

Well, first off, you are comparing applies to oranges. It shouldn’t come as much of a surprise that Imagick and Photoshop will do them differently.

Likewise, GD can do this too:

Have you tried altering your input color to Imagick to see if you can get to the color Photoshop produces?

Hi,

Thanks for the reply! I was just wondering if there was a simple effect that would allow me to do what photoshop allows.

I managed to boost the RGB values of the color and was able to get the correct output; but obviously I can’t do this for every single individual color the user inputs without knowing some kind of formula. If my original image is grayscale, and the user inputs a hex code for their color, is there a formula that can be used to calculate what values of the RGB input color need to be boosted in order to get the grayscale correctly colored?

Definitely. Can you give me the new value you used to get to the color you wanted? Then I’m sure several of us can devise algorithms to do that boost for you.

You are only overlaying a colour over the gray image which is probably not the best method.
I can not use Imagick as I do not have it installed anywhere but you may be able to modify some Imagemagick code there may be something useful on the Imagemagick forum.

An input image and the desired results are always helpful when dealing with image questions.

A test I tried from one of the forum posts using Imagemagick:

Hi guys!

Thanks for getting back to me. I decided to try and use the command tools to get what I wanted (after being recommended to do so). And the commands seem to do exactly what I needed:


$cmd = "convert  -size 600x400 xc:none \\( wolf_blue.png -fill rgb\\(51,13,13\\) -colorize 100% \\) -composite ";
$cmd .= "\\( wolf_shade.png -fill rgb\\(213,104,104\\) -colorize 100% \\) -composite ";

It allows me to shade the layer to exact color without blending the colors at all. Is this not possible with the PHP implementation? Whenever I used the PHP function for colorizeImage it would make the colors all different compared with the command line version which always produces the correct colors.

Interesting. Not sure if this helps, but I just found this, which says, using clutImage provided better results than colorizeImage.
http://stackoverflow.com/questions/8192735/imagick-colorizeimage-hex-darker (last answer)

Imagick is a limited implementation of Imagemagick and is not well supported. As it is a class they have made “assumptions” of what the user might want to do and have set some option defaults and combined some options into one.

I always use Imagemagick command line with php - see my signature - and you then have access to the full range of Imagemagick options.

Is this not possible with the PHP implementation?

I would guess you could do that with Imagick but I am not an expert with Imagick and as I say and I have no way of testing it. You would need to split it up into the individual steps.

Okay thank you guys! I think I will stick with the command line approach then!