Page 1 of 1

Use an existing pixel color for bordercolor?

Posted: 2008-02-09T16:26:37-07:00
by davidboydca
Is there a way to set a bordercolor based on an existing color at a given offset?

Using trim takes away just a little too much. I know I can specify the bordercolor manually, but I want it to figure out the color by looking at the pixel at 0,0 and use that.

This is what I would like to do:

Code: Select all

convert source.png -trim -bordercolor pixel(0,0) -border 4 output.png
Thanks!

Re: Use an existing pixel color for bordercolor?

Posted: 2008-02-10T02:07:20-07:00
by Bonzo
I do not suppose you are using php ?

If you were you could probably use http://uk.php.net/imagecolorat

Code: Select all

$border_colour = imagecolorat( image.jpg, 0, 0);

exec("convert source.png -trim -bordercolor $border_colour -border 4 output.png");

Re: Use an existing pixel color for bordercolor?

Posted: 2008-02-10T16:27:53-07:00
by davidboydca
Nope, I am doing this in an Ant script.

I did find a solution. What a hack! It uses Windows versions of grep and gawk. The result is that the ${pixel} property now contains the color for the pixel at 0,0.

<!-- This little hack gets the color of the specified pixel -->
<exec dir="." executable="imagemagik\convert.exe" failonerror="true">
<arg line='source.png'/>
<arg line=' -crop 1x1+0+0'/>
<arg line=' png24:pixel.png'/>
</exec>
<exec dir="." executable="imagemagik\convert.exe" failonerror="true">
<arg line='pixel.png TXT:pixel.txt'/>
</exec>
<exec dir="." executable="imagemagik\grep.exe" output="pixel.txt" failonerror="true">
<arg line=' ( pixel.txt'/>
</exec>
<exec dir="." executable="imagemagik\gawk.exe" output="pixel.txt" failonerror="true">
<arg line=' "{ print substr($0, match($0, /#[0-9A-F]*/), 7) }" pixel.txt'/>
</exec>
<loadfile property="pixel" srcFile="pixel.txt"/>

Re: Use an existing pixel color for bordercolor?

Posted: 2008-02-10T19:35:19-07:00
by anthony
As shown by previous posts the best way is via an API.

That is read the image and extract the color, then use that color to set the border color.

However if you are stuck for a pure single Command method, you can create a canvas of the right size using a specific color from some image.
See IM Examples, Canvas from extracted Color
http://imagemagick.org/Usage/canvas/#extract

However all you are doing is swaping the 'color' requirement to a 'size' requirement. :-(

Better to use two (or more) commands to get the info you need, or use an API.

Re: Use an existing pixel color for bordercolor?

Posted: 2008-02-11T11:53:14-07:00
by fmw42
You can also use -fx to get the color at a pixel, but you have to process the text that comes back

convert rose: -format "%[pixel:u.p{0,0}]" info:
rgb(48,47,45)


convert logo: -format "%[pixel:u.p{0,0}]" info:
white

Re: Use an existing pixel color for bordercolor?

Posted: 2008-02-19T22:55:33-07:00
by anthony
The color output from that format string should be DIRECTLY usable as a color specification for and of the color setting options. If it does NOT, then it is broken!!!