Page 1 of 1

How do I shift the hue of a black png?

Posted: 2018-11-14T14:05:10-07:00
by madvillager
hello friends!

Imagick newbie here...

having difficulty accomplishing something simple: load a black png and shift its color before outputting to screen.

platform: OSX Mojave

Code: Select all

php --ri imagick
returns

Code: Select all

imagick module => enabled
imagick module version => 3.4.3
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
Imagick compiled with ImageMagick version => ImageMagick 7.0.8-14 Q16 x86_64 2018-10-25 https://imagemagick.org
Imagick using ImageMagick library version => ImageMagick 7.0.8-14 Q16 x86_64 2018-10-25 https://imagemagick.org
etc.

my PHP:

Code: Select all

$dir = dirname(__FILE__);
$imageFolder = "/images/";
$faceImageFile = "face.png";

$face = new Imagick();
$face->readImage($dir . $imageFolder . $faceImageFile);

$face->colorizeImage('rgba(255, 0, 0, 0)', 1.0);

$face->writeImage();
header('Content-type: image/png');
echo $face;
throws an error

Code: Select all

Unrecognized color string in /Users/bolyro/Sites/crestia/setColorValue.php:12 Stack trace: #0 /Users/bolyro/Sites/crestia/setColorValue.php(12): Imagick->colorizeimage('rgba(255, 0, 0,...', 1) #1 {main} thrown in...
changing code to this

Code: Select all

$face->colorizeImage('#AA0000', 1.0);
throws same error.

i'm not married to the colorizeimage() function... simply want to shift the hue :)

btw: code works fine displaying the image on the screen if i remove the colorizeImage line. so there's apparently no problem with Imagick install.

Re: How do I shift the hue of a black png?

Posted: 2018-11-14T14:27:38-07:00
by fmw42
It would be helpful to have your input image to know exactly what you are trying to do.

Note that black has no hue, so you cannot shift its hue. If you want to replace black with some other color you can do that.

It is possible that Imagick colorizeImage does not recognize transparent colors as rgba(...). You seem to be trying to colorize with transparent red? Is that what you want?

ImageMagick command line -colorize does not colorize with transparency. For example this fails

Code: Select all

convert lena.png -alpha on -channel rgba -fill "rgba(255,0,0,0)" -colorize 100% result.png
and produces a fully opaque red image.

What you can do is colorize to opaque red and then set the alpha channel to 0 for full transparency.

Code: Select all

convert lena.png -fill "rgb(255,0,0)" -colorize 100 -alpha on -channel a -evaluate set 0 +channel result.png
You will have to look up the equivalent commands in Imagick. Sorry, I do not use that API.

Re: How do I shift the hue of a black png?

Posted: 2018-11-14T15:26:37-07:00
by madvillager
here's a visual for my question with sample graphic:

http://barrettalley.com/imagick.html

photoshop's hue/saturation filter was used to accomplish the above. with an adjustment to hue, saturation, and lightness with "colorize" turned on.

i understand @fmw42's point of not being able to colorize with transparency, but even changing my code to

Code: Select all

$face->colorizeImage('rgb(255,0,0)', 1.0);
still throws the same error... and the transparency question would be a non-issue using the hexadecimal notation that i tried as well.

i am new to imagick as well as command-line imagemagick so my question remains...

Re: How do I shift the hue of a black png?

Posted: 2018-11-14T15:41:55-07:00
by fmw42
Use the equivalent of -fill ... -opaque

Code: Select all

convert partitor.png -fill "rgba(19,123,229,1)" -opaque "rgba(0,0,0,1)" result.png
Note that you are changing opaque colors in a transparent image. So you need to specify the colors with alpha=1 for opaque.

See http://us3.php.net/manual/en/imagick.op ... timage.php

Re: How do I shift the hue of a black png?

Posted: 2018-11-14T17:20:28-07:00
by madvillager
I'm looking for a more versatile solution. As far as I understand opaquePaintImage() it's looking for a specific color to change. What if the "black png" actually contains variations of blacks (#000000, #012103) and opacities (100%, 88%)? I don't know the imagick jargon (yet), but in layman's terms the function I am looking for would "tint" or "color shift" all the colors in the png file at once. It wouldn't need a target color/opacity.

Re: How do I shift the hue of a black png?

Posted: 2018-11-14T18:03:32-07:00
by snibgo
I don't know why...

Code: Select all

$face->colorizeImage('#AA0000', 1.0);
... doesn't work. It seems to comply with the documentation. I suggest you try it with double-quotes, named colours such as red, and so on.

I suspect alternatives such as tintImage would give the same problem.

Check your software is reasonably up to date.

Re: How do I shift the hue of a black png?

Posted: 2018-11-14T19:26:17-07:00
by fmw42
What if the "black png" actually contains variations of blacks (#000000, #012103) and opacities (100%, 88%)?
You add -fuzz, which I believe is built into the opaquePaintImage()

Re: How do I shift the hue of a black png?

Posted: 2018-11-14T19:53:12-07:00
by madvillager
colorizeImage() and tintImage() are working now!

I had to add a third parameter "true" like this:

$image->tintImage('rgba(255,0,0)', 1, true);

@Damack addresses this issue here: https://github.com/mkoppanen/imagick/issues/182

(Though I still don't understand why I needed "true"... perhaps because I have the latest version of imagick?)

and thank you @fmw42 - I see the -fuzz parameter now for opaquePaintImage().