Set Border Color from Image

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
twenck
Posts: 2
Joined: 2018-01-02T17:19:25-07:00
Authentication code: 1152

Set Border Color from Image

Post by twenck » 2018-01-02T17:32:48-07:00

This is probably clearly documented but after hours of searching the docs, examples, and forum... nothing.

Version 7.0.7-6 Windows 10

I have a large number of small (88x88) PNG images to which I wish to add a border. I want the border color to be a color near the edge of the existing image, say pixel 88,88.

The following works correctly to add a 5 pixel black border to the images:

magick mogrify -bordercolor black -border 5 *.png

How can I specify the bordercolor to be whatever color is in the image at pixel 88x88, rather than black?


Thanks in advance, & sorry for the bother for something so simple,

Tom

User avatar
fmw42
Posts: 23429
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Set Border Color from Image

Post by fmw42 » 2018-01-02T17:57:31-07:00

You will have to loop over each image in your directory using IM 7 magick as follows:

Code: Select all

magick image.png -set option:bcolor "%[pixel:u.p{87,87}]" -bordercolor "%[bcolor]" -border 5 image.png
Unfortunately, magick mogrify does not permit the use of -set option as above with % escapes as best I can tell. Using -set option with % escapes is only permitted in magick in IM 7 and not in magick with mogrify, montage, etc.

So you cannot do:

magick mogrify -format png -set option:bcolor "%[pixel:u.p{87,87}]" -bordercolor "%[bcolor]" -border 5 *.png

Thus you will need to write a script loop over each image in your directory. That will be OS dependent and I am not a Windows user. So I cannot help. I could show you in Unix, but not Window.

See
http://www.imagemagick.org/Usage/transform/#fx_other
https://legacy.imagemagick.org/script/fx.php

twenck
Posts: 2
Joined: 2018-01-02T17:19:25-07:00
Authentication code: 1152

Re: Set Border Color from Image

Post by twenck » 2018-01-02T21:08:35-07:00

Thank you for the feedback.

If I have the RGB values of the color desiredfor groups of images, and did not need to sample the images, is there a way to use the mogrify command to batch process the images, adding a 5 pixel border of that color?

I guess, something like:

magick mogrify -bordercolor (insert known color argument here) -border 5 *.png


Tom

User avatar
fmw42
Posts: 23429
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Set Border Color from Image

Post by fmw42 » 2018-01-02T21:30:26-07:00

twenck wrote:
2018-01-02T21:08:35-07:00
Thank you for the feedback.

If I have the RGB values of the color desiredfor groups of images, and did not need to sample the images, is there a way to use the mogrify command to batch process the images, adding a 5 pixel border of that color?

I guess, something like:

magick mogrify -bordercolor (insert known color argument here) -border 5 *.png


Tom

Yes, that will work. But I suggest you create a new directory and use the -path option to put all the output image into that directory rather than writing over or into the same directory as the input images. See http://www.imagemagick.org/Usage/basics/#mogrify

User avatar
GeeMack
Posts: 591
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Set Border Color from Image

Post by GeeMack » 2018-01-02T21:53:56-07:00

twenck wrote:
2018-01-02T17:32:48-07:00
I have a large number of small (88x88) PNG images to which I wish to add a border. I want the border color to be a color near the edge of the existing image, say pixel 88,88.

How can I specify the bordercolor to be whatever color is in the image at pixel 88x88, rather than black?
With IM 7.0.7-19 at a Windows 10 CMD prompt, I can run a command like this...

Code: Select all

magick *.jpg -set filename: "bordered_%[t]" -border 5 -strokewidth 10 ^
    -fill none -draw "stroke #%[hex:s.p{88,88}] rectangle -1,-1 %[fx:s.w],%[fx:s.h]" "%[filename:].jpg"
That will read in all the JPG images in the directory and set an output name for each by prefixing "bordered_" to the input file name. Then it adds a 5 pixel border to every image. Then it draws a rectangle around each image using the color of the pixel found at position 88x88 in that particular image. The result is a uniquely colored 5 pixel border on each image, with output files named "bordered_inputname1.jpg"... etc.

This could get slow if there are very many images or if they're very large, but you should easily be able to run thousands of your 88x88 images.

To use a command like that in a BAT script you'd need to change all the single percent signs "%" to doubles "%%".
Last edited by GeeMack on 2018-01-02T22:01:56-07:00, edited 1 time in total.

User avatar
fmw42
Posts: 23429
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Set Border Color from Image

Post by fmw42 » 2018-01-02T22:01:14-07:00

GeeMack: Nice concept using % escapes in the draw command!

Note, if the images are 88x88, then a coordinate of 88,88 is off the image. So it should be 87,87. OP: Is there any particular reason to use the last pixel in the image rather than the first at 0,0?

Also in GeeMack's command, I believe that -draw will not draw outside the bounds of the input image. -draw only draws inside the image. Furthermore, you should draw offset by half the desired border width.

Alternately, you could extend the image using -bordercolor black and then draw over the extended part. So for example:

input (256x256):
Image

Code: Select all

magick lena.png -bordercolor black -border 10 -strokewidth 10 -stroke red -fill none -draw "rectangle 5,5 270,270" -alpha off lena_border.png
Image

User avatar
GeeMack
Posts: 591
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Set Border Color from Image

Post by GeeMack » 2018-01-02T22:25:21-07:00

fmw42 wrote:
2018-01-02T22:01:14-07:00
Note, if your images are 88x88, then a coordinate of 88,88 is off the image. So it should be 87,87.
Thanks for catching that. In this example the pixel from the input image is checked after the 5 pixel border is added, so the top left pixel from the input image would be 5,5, and the bottom right would be 92,92.

User avatar
fmw42
Posts: 23429
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Set Border Color from Image

Post by fmw42 » 2018-01-02T22:31:42-07:00

GeeMack, see my addition comments above. You can easily modify your command to do what I have shown.

User avatar
GeeMack
Posts: 591
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Set Border Color from Image

Post by GeeMack » 2018-01-02T22:40:33-07:00

fmw42 wrote:
2018-01-02T22:01:14-07:00
Alternately, you could extend the image using -bordercolor black and then draw over the extended part.
Yep. In my example I used "-border 5" to add 5 pixels all around, and left it at default border color because it's getting covered by the rectangle's stroke anyway. The "-strokewidth 10" is twice the border width because only half the stroke around the rectangle will be inside the finished image.

User avatar
fmw42
Posts: 23429
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Set Border Color from Image

Post by fmw42 » 2018-01-02T22:47:02-07:00

OK. I see that now. But it might be cleaner to add -border 10 and then draw your stroke width 10 to cover the added amount. With odd pixel stroke widths, -draw will draw at the center of the width, which would be in your case at 2.5 in from the edge, so -1,-1 may not be correct.

Post Reply