Composing clone over modified original image - needs second clone?

Viewed 108

I have an image I'd like to put a white border around, covering the original image. However, unlike questions like this, I want to do it with a percentage of the image size. This means that -shave then -border doesn't work, as removing, say, 10% and adding it back results in a slightly smaller image (100 × 0.9 × 1.1 = 99)

So, my approach is to -clone the image, -shave it, set the original image to white and then compose it back together.

However, the only way I've been able to do this is with two clones (one to turn white and one to shave) and then -deleteing the original image before composing:

convert in.png -gravity center \
    \( -clone 0 -evaluate set 100% \) \
    \( -clone 0 -shave 10%x10% \) \
    -delete 0 -composite out.png

This works, but I'm wondering if I can do it without the second -clone and the -delete 0. Something like the following (which doesn't work - the output is solid white):

convert in.png -gravity center \
    \( -clone 0 -shave 10%x10% \) \
    -evaluate set 100% -composite out.png
3 Answers

If you're using ImageMagick v7 you can use percent escapes like "%G" to substitute for the image's original dimensions as Mark described above. Many of those escapes and FX expressions aren't available to use if you're running IMv6. In that case here is another method for accomplishing your task...

convert in.png -write mpr:img0 -evaluate set 100% \
  \( mpr:img0 -shave 10% \) -gravity center -composite out.png

That reads the input image, stores a copy of it in a memory register "mpr:img0", then turns the input image white. Inside the parentheses it brings that memory copy back and shaves the 10% off all the edges. After the parentheses it composites that shaved image centered over that all-white image and writes the output.

Edited to add:

Here is a link to information about using the memory register "mpr:". It can often be used as a way to reduce the number of clone operations or to modify the order of the current list of images...

IM file format "mpr:"

Here is a link to more information about IM's percent escapes...

IM percent escapes

You can use -extent with %G which represents the original geometry, like this to set the "border inside" as Photoshop calls it:

magick  paddington.png -shave 10x10% -gravity center -background magenta -extent "%G" result.png

which transforms this:

enter image description here

into this:

enter image description here


For the sake of completeness and future reference, you can set the "border outside" to a percentage of the original width and height like this:

magick  paddington.png -bordercolor yellow  -border "10%"  outside.png

enter image description here

This will add 10% of the original width left and right, and 10% of the original height top and bottom. Omit the percent sign if you just want 10 pixels absolute border width.


Keywords: Image processing, ImageMagick, border inside, border outside.

Accepted solutions have been offered, but there are almost always multiple ways to accomplish a given task with ImageMagick. Often one method may be preferred over another depending on particular work flow, etc. So here is another way to apply a border area to an image using percentages rather than number of pixels...

convert in.png -gravity center -crop 80x80%+0+0 -flatten out.png

This crops the center 80% horizontally and vertically, essentially removing 10% from every side. So instead of describing the borders as a percentage of the input image, the percentages describe the image that remains inside the borders.

The cropped image maintains its original dimension and offset data, so "-flatten" will place the image back in its original location on a canvas of the original dimensions.

Since IM's default background color is white, the new borders will be white. Use -background somecolor before the flattening to change it.

This command is in IMv6. It should work identically with IMv7 using "magick" instead of "convert".

Related