How to Add a Border to an Image with ImageMagick?
Adding a border to an image is a common task in image processing that
can be easily accomplished using the ImageMagick convert
command. This article provides a quick overview of how to use the
-bordercolor and -border settings to define
the color and size of your border. We will cover the basic syntax, how
to apply uniform borders, and how to specify different widths for the
horizontal and vertical edges.
Basic Syntax for Adding Borders
To add a border using ImageMagick, you need to chain two primary
options together: -bordercolor to define the color, and
-border to define the thickness. The order matters; you
must specify the color before applying the border geometry.
The standard command structure looks like this:
convert input.jpg -bordercolor black -border 10 output.jpg
Specifying Border Dimensions
The -border option accepts dimensions in pixels. You can
control the thickness in two ways:
- Uniform Borders: Providing a single number (e.g.,
-border 10) applies a 10-pixel border to all four sides of the image. - Asymmetric Borders: Providing two numbers separated
by an “x” (e.g.,
-border 20x10) applies different widths. The first number represents the horizontal border (added to the left and right sides), and the second number represents the vertical border (added to the top and bottom).
Choosing Border Colors
ImageMagick is highly flexible with color selection. You can define colors using standard names or hex codes:
- Named Colors: You can use common color names like
white,black,red, orblue. - Hex Codes: For precise color matching, use hex
codes enclosed in quotes, such as
-bordercolor "#3357FF".
Example Configurations
Here is a quick reference table showing how different command variations affect the final image output:
| Desired Effect | Command Example |
|---|---|
| Simple 5px black border | convert input.jpg -bordercolor black -border 5 output.jpg |
| Thick 20px white border | convert input.jpg -bordercolor white -border 20 output.jpg |
| Asymmetric border (15px sides, 5px top/bottom) | convert input.jpg -bordercolor gray -border 15x5 output.jpg |
| Custom hex color border | convert input.jpg -bordercolor "#FF5733" -border 10 output.jpg |
By mastering these basic parameters, you can efficiently batch-process images or integrate border creation into automated web workflows and scripts.