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:

Choosing Border Colors

ImageMagick is highly flexible with color selection. You can define colors using standard names or hex codes:

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.