using fx to add a constant to each rgb pixel

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
yapposai
Posts: 3
Joined: 2013-12-12T08:05:25-07:00
Authentication code: 6789

using fx to add a constant to each rgb pixel

Post 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.
yapposai
Posts: 3
Joined: 2013-12-12T08:05:25-07:00
Authentication code: 6789

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

Post 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;
}
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo »

fx is generally slow. The quickest way is probably "-evaluate add 50%". I don't know how to do that in PHP.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
yapposai
Posts: 3
Joined: 2013-12-12T08:05:25-07:00
Authentication code: 6789

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

Post by yapposai »

Thank you for your replies,

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