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
Add a border without increasing the size of the image
- 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
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
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
- 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
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/
https://imagemagick.org/Usage/
Re: Add a border without increasing the size of the image
Anthony, do you not have to use repage after shave because you are adding some pixels back immediately with the border command?
Dave
Dave
- 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
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.
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/
https://imagemagick.org/Usage/