How to add a border using Imagemagick
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.
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.