Page 1 of 1

Add a border without increasing the size of the image

Posted: 2009-04-10T16:16:41-07:00
by daviddoria
Is there a way to take a 100x100 image and add a 2 pixel border and keep the resulting image 100x100 (rather than 104x104)? Clearly I could just resize it back to 100x100 after the border operation, but I didn't know if it could be done in one step.

Dave

Re: Add a border without increasing the size of the image

Posted: 2009-04-10T17:43:13-07:00
by fmw42
see -shave and -extent

http://www.imagemagick.org/Usage/crop/#shave
http://www.imagemagick.org/Usage/crop/#extent

to remove and add 1 pixel border (skyblue) from a 100x100 image

Code: Select all

convert image -shave 1x1 +repage -background skyblue -gravity center -extent 100x100 result

Re: Add a border without increasing the size of the image

Posted: 2009-04-10T22:16:21-07:00
by anthony
To replace the 2 pixels along the edge without needing to know the image size use -shave and -border

Code: Select all

convert image.png  -shave 2x2  -bordercolor black -border 2x2  image_border.png

Re: Add a border without increasing the size of the image

Posted: 2009-04-11T04:40:24-07:00
by daviddoria
Anthony, do you not have to use repage after shave because you are adding some pixels back immediately with the border command?

Dave

Re: Add a border without increasing the size of the image

Posted: 2009-04-12T01:51:31-07:00
by anthony
neither border or shave handle image 'layering' pixels are directly added or removed from the image proper without reference to 'layers'.

However the 'canvas size' of the layer image is adjusted with the appropriate number of added and removed pixels, to ensure the virtual canvas size remains correct. That is the only concession to layers made.

The same goes for 'chop' and 'splice'. Only 'crop' preserves layering information to ensure pixels do not move just because you cut them out part of a larger (possibility layered) image.

I though the IM Examples was clear on this, but perhaps not.