15

I'm want to add to my pictures in Imagemagick:

  • white colored border
  • 2 inches wide

How can I do it?

3 Answers 3

20
convert -bordercolor white -border 20 input.jpg output.jpg

will add a white border of size 20 pixels to image. You just need to find out how much px is an inch (dpi) of image.

More examples can be found here.

In case of my test image, it had saved dpi info in it. I could get it using identivy -verbose and it look like this:

$ identify -verbose tiger.jpg
Image: tiger.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: DirectClass
  Geometry: 600x400+0+0
  Resolution: 96x96
  Print size: 6.25x4.16667
  Units: PixelsPerInch
  Type: TrueColor
...

As you can see, it has 96x96 dpi as units are of type PixelsPerInch.

In that case 2 inches on my image is 192px.

3
  • I want to add 2 inches, how can ?
    – Deniz
    Sep 1, 2016 at 9:29
  • if your image have somehow saved dpi info in it, you should be able to get it using identify -verbose. Images normally do not have that info
    – V-master
    Sep 1, 2016 at 9:36
  • For available colours see here: imagemagick.org/script/color.php Jan 21, 2021 at 16:55
2

First, find out how many pixels the 2" border will have. Add these pixels to the #pixels of your original Image (twice, as you want the same border on each side).

Now use convert like

convert <input> -size <newsize> xc:white +swap -gravity center -composite <output>

Let's say, the image size is 3600x2400, and the resolution is 100px/in. So you have to add 400px to each dimension, getting 4000x2800 as the result. (How to find out these values has been described by @V-master). Then the command reads (where input.jpg is the original and output.jpg is the result)

convert input.jpg -size 4000x2800 xc:white +swap -gravity center -composite output.jpg

For me this worked as requested.

Edit:

convert input.jpg -bordercolor white -border <n> output.jpg

(as already suggested by @V.master) works as well, only -bordercolor option has to be defined before -border. It is a shortcut for my suggestion, which I use to have borders of different size (rsp. fixed aspect ratio independent of that of the original image).

There is no option to set the unit to inches. What 2" are depends on the output resolution of your picture when being printed (eg. 300dpi: n=600, 600dpi: n=1200 etc.).

For those wondering: the xc non-option stands for X window Color, and sets the canvas color. In newer versions of ImageMagick the canvas alias has been added for clarity.

1
  • Does it keep the image dimensions or the border goes outside?
    – Sigur
    Feb 22, 2019 at 14:49
0

While working on documentation, I have to update images and add frame again.

The following script makes adding frame easy:

#!/bin/bash -e
if [ -z "$1" ]; then
    echo "Usage: $0 file"
    exit 1
fi

file="$1"
if [[ "$file" == *"-original"* ]]; then
    original=$file
    file=${file//-original/}
else
    extension=${file##*.}
    original="${file%.$extension}-original.$extension"
fi

if [ ! -e "$original" ]; then
    mv "$file" "$original"
fi

convert -bordercolor white -border 2 "$original" "$file"

For example, we have a file myimage.png with no frame. The following command:

$ ./add_frame.sh myimage.png

For file myimage.png:

  1. creates myimage-original.png with current (no frame) content
  2. adds frame to myimage.png

For the next runs:

$ ./add_frame.sh myimage.png # you may use current name

$ ./add_frame.sh myimage-original.png # or with original suffix, result is the same
  1. It copies myimage-original.pngmyimage.png
  2. Add frame to myimage.png

Thus I just update myimage-original.png and add frame via script.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .