Add a border without increasing the size of the image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
daviddoria

Add a border without increasing the size of the image

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
daviddoria

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

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply