Page 1 of 1

using fx to add a constant to each rgb pixel

Posted: 2013-12-12T08:27:13-07:00
by yapposai
Hi,

I'm looking for a way to add a constant value to each of the rgb pixel of an image. I'm currently using the fxImage function in php.
The reason I want to do this is am trying to change the brightness of the image similar to how my html5 javscript library does it.

I can do what I want using
  • $img = $img->fxImage("r+0.5",Imagick::CHANNEL_RED);
    $img = $img->fxImage("g+0.5",Imagick::CHANNEL_GREEN);
    $img = $img->fxImage("b+0.5",Imagick::CHANNEL_BLUE);
but it takes 3 separate operations which slows things down. I am hoping to do the operation once to gain some speed (I am working with raspberry pi which has a slow cpu,limited resources)

I tried using something like
  • $img = $img->fxImage("rgb(r+0.5,g+0.5, b+0.5)");
but all I get is a black image. What I don't understand is if I do
  • $img = $img->fxImage("rgb(255,255, 0)")

the image is as expected (plain yellow box )

I tried using "rgb(r/255,g/255, b/255)" or "rgb(r*255,g*255, b*255)" but I still get a black box.

Is my understanding correct that it seems you can't use the r,g,b values inside the rgb() function of the fx operator?

Any help would be appreciated , thank you.

Re: using fx to add a constant to each rgb pixel

Posted: 2013-12-12T09:15:23-07:00
by yapposai
I gave up on the fx function and just did the following:

1.create a new grayscale image for the amount you want to lighten/darken
2. use composite_plus or composite_minus to darken or lighten the image.
it seems much faster.

$tmp = new Imagick();
$tmp->newImage($img->getImageWidth(), $img->getImageHeight(), new ImagickPixel("rgb($brightness_param,$brightness_param,$brightness_param)"));
$tmp->setImageFormat('png');

if($lighten_flag>0){
$img->compositeImage($tmp,Imagick::COMPOSITE_PLUS,0, 0);
}else{
$tmp->compositeImage($img,Imagick::COMPOSITE_MINUS,0, 0);
$img = clone $image;
}

Re: using fx to add a constant to each rgb pixel

Posted: 2013-12-12T11:11:30-07:00
by snibgo
fx is generally slow. The quickest way is probably "-evaluate add 50%". I don't know how to do that in PHP.

Re: using fx to add a constant to each rgb pixel

Posted: 2013-12-12T11:27:35-07:00
by fmw42
see -color-matrix at

http://www.imagemagick.org/script/comma ... lor-matrix

it used to be called -recolor, but had a more restrictive matrix that may not have allowed addition, only multiplication. I don't recall.

see
http://us2.php.net/manual/en/imagick.recolorimage.php

Otherwise, use -evaluate add XX% for each channel. See http://us2.php.net/manual/en/imagick.evaluateimage.php

Re: using fx to add a constant to each rgb pixel

Posted: 2013-12-12T17:54:15-07:00
by yapposai
Thank you for your replies,

I'll investigate the recolor and evaluateImage and see what I come up with.