How to Set Background Color When Rotating in ImageMagick?

When rotating an image using ImageMagick’s convert command, the software automatically fills the empty corner areas created by the rotation with a default color (usually white or transparent). To customize this, you must use the -background setting before applying the -rotate operator. This article provides a quick overview and practical examples of how to specify a custom background color—including hex codes, named colors, and transparency—during image rotation.

The Correct Command Structure

In ImageMagick, the order of operations matters. You must define the background color first so that the rotation command knows what color to use when filling the newly created canvas space.

The basic syntax looks like this:

magick convert input.jpg -background color_name -rotate degrees output.jpg

Note: If you are using ImageMagick v7 or newer, the convert command is replaced by the unified magick command, though magick convert still works for backwards compatibility.

Examples of Different Color Formats

You can specify the background color in several ways depending on your project’s needs:

magick convert photo.png -background blue -rotate 45 rotated_blue.png
magick convert photo.png -background "#ff5733" -rotate 30 rotated_hex.png
magick convert photo.png -background none -rotate 15 rotated_transparent.png

Troubleshooting Common Mistakes

If your background color is still showing up as the default white or black, double-check these two common issues: