Can ImageMagick Convert Draw Circles or Ellipses?
Yes, the ImageMagick convert command (or the
magick command in newer versions) can easily draw circles
and ellipses onto an existing image using the -draw
operator. This capability is highly useful for automated image
processing, such as highlighting specific areas of a photo, creating
custom avatars, or generating geometric overlays. By leveraging
coordinates and radius values, you can precisely control the placement,
size, border width, and color of the shapes you render onto your base
image.
How to Draw a Circle
To draw a circle on an existing image, you use the -draw
option followed by the circle primitive. The
circle primitive requires two sets of coordinates: the
center point $(x_0, y_0)$ and a point on the
perimeter $(x_1, y_1)$. ImageMagick automatically
calculates the radius based on the distance between these two
points.
Here is the basic syntax:
magick input.jpg -fill None -stroke red -strokewidth 3 -draw "circle 150,150 150,200" output.jpg
In this example:
- -fill None makes the inside of the circle
transparent. You can change this to a color (e.g.,
-fill blue) to create a solid shape. - -stroke red sets the border color to red.
- -strokewidth 3 sets the thickness of the border to 3 pixels.
- circle 150,150 150,200 places the center of the circle at $(150, 150)$ and extends the radius to $(150, 200)$, resulting in a circle with a 50-pixel radius.
How to Draw an Ellipse
If you need a squished or elongated shape rather than a perfect
circle, you can use the ellipse primitive. Unlike the
circle primitive, the ellipse primitive requires the center
point, the x and y radii, and the
starting and ending degrees of the arc (which allows
you to draw partial ellipses or full 360-degree shapes).
Here is the basic syntax for a full ellipse:
magick input.jpg -fill yellow -stroke black -strokewidth 2 -draw "ellipse 200,150 80,40 0,360" output.jpg
Breaking down the ellipse parameters:
- 200,150 defines the center point $(x, y)$ of the ellipse.
- 80,40 defines the horizontal radius ($80$ pixels wide) and the vertical radius ($40$ pixels tall).
- 0,360 defines the start and end angles. Using
0,360renders a complete, closed ellipse.
Best Practices and Tips
When drawing shapes in ImageMagick, keep these quick tips in mind for the best results:
- ImageMagick v7 Syntax: In ImageMagick version 7 and
later, the
converttool is replaced by the unifiedmagickcommand, thoughconvertis still supported for backward compatibility. - Coordinate System: ImageMagick’s coordinate system starts at $(0,0)$ in the top-left corner of the image. Increasing $x$ moves right, and increasing $y$ moves down.
- Opacity Control: You can use RGBA color values to
make your shapes semi-transparent. For example,
-fill "rgba(0,0,255,0.5)"will fill the shape with a 50% transparent blue color, allowing the background image to show through.