Set Border Color from Image
Set Border Color from Image
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
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
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Set Border Color from Image
You will have to loop over each image in your directory using IM 7 magick as follows:
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
Code: Select all
magick image.png -set option:bcolor "%[pixel:u.p{87,87}]" -bordercolor "%[bcolor]" -border 5 image.png
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
Re: Set Border Color from Image
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
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
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Set Border Color from Image
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
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Set Border Color from Image
With IM 7.0.7-19 at a Windows 10 CMD prompt, I can run a command like this...twenck wrote: ↑2018-01-02T17:32:48-07:00I 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?
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"
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Set Border Color from Image
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):
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):
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
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Set Border Color from Image
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Set Border Color from Image
GeeMack, see my addition comments above. You can easily modify your command to do what I have shown.
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Set Border Color from Image
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Set Border Color from Image
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.