Page 1 of 1
Replace color and its tones or variantion
Posted: 2014-04-10T06:13:18-07:00
by lp1051
Hi,
I'm rather beginner with imagick, but I thought I should manage this task with all the resources online, but I failed... I need to replace color of this frame:
with any chosen hex color, lets say red, #ff0000
So far, the best I could do was only to replace the original color, using some low fuzz distance with the red using this cmd:
Code: Select all
convert original.jpg -fuzz 2% -fill "#ff0000" -opaque "#cddcc5" replaced.jpg
So I get this:
The problem is that the round corners need to have different tone of the red, in order to look smooth, just like the original has a different tone of the light green. So my question is how can I apply the color replacement using some kind of tone-aware technique, rather than only red color for everything that fits the color to replace and fuzz distance?
Thanks a lot for any advice,
Luk
Re: Replace color and its tones or variantion
Posted: 2014-04-10T07:28:44-07:00
by snibgo
Code: Select all
convert lfISkp3.jpg -channel RGB -auto-level +level-colors red,white out.png
Re: Replace color and its tones or variantion
Posted: 2014-04-10T09:39:30-07:00
by lp1051
That's brilliant!! It's exactly what I need. Thank you very much for help!
The only problem left is how to achieve this with PHP5 wrapper... I can find neither auto-level nor level-colors there. Does anybody know how to rewrite the cmdline using the php5-imagick extension? Or perhaps another way of doing the same?
Re: Replace color and its tones or variantion
Posted: 2014-04-10T10:41:12-07:00
by snibgo
Sorry, I don't know. But I've heard that PHP has an exec() function that runs commands.
Re: Replace color and its tones or variantion
Posted: 2014-04-10T10:49:52-07:00
by lp1051
Yes, I know, but our server administrator that didn't want to allow me to install imagick, only the php5 wrapper. I thought it will be enough for what I need so I didn't fight, but I guess I will...
Re: Replace color and its tones or variantion
Posted: 2014-04-10T10:57:33-07:00
by fmw42
I am not an expert on Imagick and would recommend you use PHP exec(). Imagick has not kept up with Imagemagick and does not seem to be well supported any longer.
However, you can replace -auto-level with -contrast-stretch 0. See
http://us3.php.net/manual/en/imagick.co ... himage.php
I do not see any equivalent to +level-colors, which is odd since it has been around for a long time. Perhaps I am missing it. Nevertheless, you can create a color gradient between the two desired colors and apply that color gradient with -clut. See
http://us3.php.net/manual/en/imagick.clutimage.php
Re: Replace color and its tones or variantion
Posted: 2014-04-10T14:53:01-07:00
by lp1051
No expert, but you couldn't give me better answer:) Thanks a lot! I followed your advice and it's almost there. I'm not sure it is good idea to put the PHP code here, as it is forum for imagemagick, but maybe you will see what mistake I made, or maybe it will help others with the same struggle.
First, I found out the quantum range is in my case (might be same for everybody, not sure) 65535, so I tried to simulate -contrast-stretch 0 with black point = 0, white point = quantum range and default for all channels. I experimented with only single channels, but it didn't help really. Then I created the red-white gradient and applied it with clut.
Code: Select all
$img = new Imagick('original.jpg');
$img->contrastStretchImage(0, 65535, Imagick::CHANNEL_ALL);
$clut = new Imagick();
$clut->newPseudoImage(1, 10, "gradient:red-white");
$img->clutImage($clut);
The result has smooth corners, that's good, but the color is not red. It gives me #ff2d00. It seems to be because of the contrastStretchImage() method, which is giving different result than when I run -contrast-stretch 0 in cmdline. I don't know why, I think I tried all possible combinations. When I set the black point to 30000 (keep white point same) it gives me the red, but you can see some yellowish lines at some edges. With even higher value for black point it starts to lose info, and the corners looks like in my first post. So I'm really out of ideas what more could I try.
If you see some obvious stupid error in my attempts, please correct me. If not, I really appreciate the help from both of you and I will try to fix imagemagick on the server instead.
Thank you again!
Re: Replace color and its tones or variantion
Posted: 2014-04-10T14:58:45-07:00
by fmw42
$img->contrastStretchImage(0, 65535, Imagick::CHANNEL_ALL);
use
$img->contrastStretchImage(0, 0, Imagick::CHANNEL_ALL);
It works for stretching by percent counts from either end of the histogram starting with the min and max bins, might not be 0 and quantumrange.
see
http://www.imagemagick.org/script/comma ... st-stretch
Always a good idea to check the command line options for descriptions.
You might also want to make your gradient image longer in size to get better interpolation accuracy, say 256 in length
Re: Replace color and its tones or variantion
Posted: 2014-04-10T15:08:54-07:00
by lp1051
Yes, I was reading it, but using both points as zero, gives me a lime color of the frame and the image already lost its accuracy (the round corners are not round anymore). And when I apply the clut, the result color is yellow... And I tried different size of gradient as well, 500x500 was max:)
Re: Replace color and its tones or variantion
Posted: 2014-04-10T15:30:17-07:00
by fmw42
Sorry I missed this, but you must convert your input image first to grayscale in order to do +level-colors or your equivalent. Also looking at the Imagick code for contrastStretchImage, they appear to be using non-percent gray values and the older syntax, where you use 0 and quantumrange.
However, your min and max graylevels in the image may be somewhere inbetween. Check the verbose information (identify -verbose yourimage) to see what are the min and max values and try those. But the main issue is you need to convert to grayscale.
try
$img = new Imagick('original.jpg');
$img->modulateImage(0, 100, 100);
$img->contrastStretchImage(0, 65535, Imagick::CHANNEL_ALL);
$clut = new Imagick();
$clut->newPseudoImage(1, 2, "gradient:red-white");
$img->clutImage($clut);
change 0 and 65535 to min and max graylevel if different and the above does not work.
Re: Replace color and its tones or variantion
Posted: 2014-04-10T16:27:17-07:00
by lp1051
Yes, indeed, that did the trick! Using grayscale and 0, quantumrange for contrast stretch, gave me #fe0000, and rounded corners. Same results as auto-level and level-colors, so unbelievable achievement, when I think about few hours ago. Thousands of thanks:)
Only small update for the grayscale:
Code: Select all
$img->modulateImage(100, 0, 100);
I also ran
on the grayscaled image. If you meant to try with min and max from Histogram section, for greyxx I got 1 min and 28560 max (btw. max for white was 123296, weird?), but using those grey min/max values gave me light pink color instead. I also tried the min max from image overall statistics (min 208, max 255), and it gave me the same light pink colour, so I'm not really sure if I you meant I should run it on the original image, and I don't understand how can be the white outside quantum range...
But anyway, the current result is more than satisfying, I can go to bed with a smile. Good night:)
Re: Replace color and its tones or variantion
Posted: 2014-04-10T17:23:35-07:00
by fmw42
Sorry, meant to reduce the saturation. I misread the PHP Imagick manual. You are correct that it should be