Use an existing pixel color for bordercolor?

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
davidboydca

Use an existing pixel color for bordercolor?

Post 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!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Use an existing pixel color for bordercolor?

Post 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");
davidboydca

Re: Use an existing pixel color for bordercolor?

Post 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"/>
Last edited by davidboydca on 2008-02-12T20:05:07-07:00, edited 1 time in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Use an existing pixel color for bordercolor?

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Use an existing pixel color for bordercolor?

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Use an existing pixel color for bordercolor?

Post 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!!!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply