opsb
opsb

Reputation: 30231

Add border to top of image using imagemagick

I have an image 100x40 and I want to add a border to the top of the image 10 pixels high.

I've found

convert source.jpg -border 0x10 out.jpg

but this adds a border to the top and bottom. Is there anyway to add it to only the top?

Upvotes: 24

Views: 17774

Answers (3)

Tested as of 6.9.11, the default background is now white, not transparent, at least for PNG input/output.

To get a transparent background you need something like:

convert source.png -gravity north -background transparent -splice 0x10 out.png

north adds on top, south on bottom.

Also note that -background has to come before -splice.

Demo JPG input:

wget -O chick.jpg https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Hühnerküken_02.jpg/800px-Hühnerküken_02.jpg

Unmodified input preview:

enter image description here

100 px white border on top with -gravity north (white is the default color):

convert chick.jpg -gravity north -splice 0x100 chick-default.jpg

enter image description here

100 px black border on top with -background black:

convert chick.jpg -gravity north -background black -splice 0x100 chick-black.jpg

enter image description here

Black border on bottom with -gravity south:

convert chick.jpg -gravity south -background black -splice 0x100 chick-black-south.jpg

enter image description here

Black border on both top and bottom with a double -gravity + -splice:

convert chick.jpg -background black -gravity south -splice 0x100 \
  -gravity north -splice 0x100 chick-black-north-south.jpg

enter image description here

100px black border all around image instead with -border as per https://askubuntu.com/questions/819482/how-to-add-a-border-using-imagemagick

convert -bordercolor black -border 100 chick.jpg chick-around.jpg

enter image description here

Upvotes: 3

Danilo Piazzalunga
Danilo Piazzalunga

Reputation: 7844

Use -splice:

convert source.jpg -splice 0x10 out.jpg

If you want to add the border only at the bottom, use -gravity as well: convert source.jpg -gravity south -splice 0x10 out.jpg

Note that the border will be transparent, unless you use -background, too.

See also Cutting and Bordering for more examples.

Upvotes: 23

eduffy
eduffy

Reputation: 40262

Use -extent instead:

convert source.jpg -gravity south -extent 100x50 out.jpg

-gravity tell it which direction to move the original image.

Upvotes: 5

Related Questions