Annotating Images from the Command-Line

Annotating Images from the Command-Line

You can use ImageMagick to perform basic image annotation from the command-line. First, install it if necessary with one of:

$ apt install imagemagick
$ brew install imagemagick
$ wget https://imagemagick.org/download/binaries/magick

You should now have the convert binary available:

$ convert --version
Version: ImageMagick 7.0.9-16 Q16 x86_64 2020-01-12 https://imagemagick.org
Copyright: © 1999-2020 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(3.1)
Delegates (built-in): bzlib freetype heic jng jp2 jpeg lcms ltdl lzma openexr png tiff webp xml zlib

The remainder of this post will use this source image:

Photo by Kate Hliznitsova on Unsplash

Rectangles

Annotate the image with a rectangle with:

$ convert -fill none -stroke white -strokewidth 15 \
          -draw "rectangle 1473 682 2020 1181" \
          source.jpg out.jpg

Note that the arguments to the rectangle sub-command are x1 y1 x2 y2, where (x1,y1) are coordinates of the top-left corner of the rectangle, and (x2,y2) are the coordinates of the bottom-right corner, assuming that the top-left corner of the image itself is (0,0).

You can compose these commands to draw multiple rectangles:

$ convert -fill none -stroke white -strokewidth 15 \
          -draw "rectangle 1473 682 2020 1181" \
          -draw "rectangle 1814 1101 2385 1645" \
          -draw "rectangle 466 1056 1431 2006" \
          -draw "rectangle 994 2083 1561 2661" \
          source.jpg out.jpg

Other Shapes

You can also use -draw to annotate the image with other shapes:

$ convert -fill none -stroke white -strokewidth 15 \
          -draw "rectangle 1473 682 2020 1181" \
          -draw "roundRectangle 1814 1101 2385 1645 50 50" \
          -draw "circle 906 1492 345 1527" \
          -draw "polygon 947 2193 1111 2020
                         1466 2021 1605 2397
                         1454 2631 1064 2656" \
          source.jpg out.jpg

Some of these shapes are specified differently. For example:

  • circle accepts two sets of coordinates, the center of the circle, and any point on its circumference.
  • polygon accepts 3 or more coordinates, representing its vertices.

Finally, -draw supports other shape types like arc and ellipse; here's a full list.

Fills

Shapes can be filled-in:

$ convert -fill '#00000080' -stroke white -strokewidth 15 \
          -draw "rectangle 1473 682 2020 1181" \
          -fill '#FF000080' -stroke green -strokewidth 15 \
          -draw "roundRectangle 1814 1101 2385 1645 50 50" \
          -fill '#00FF0080' -stroke blue -strokewidth 15 \
          -draw "circle 906 1492 345 1527" \
          source.jpg out.jpg

Text

Add text to an image with -annotate:

$ convert -fill white -font Helvetica-Bold -pointsize 250 \
          -annotate +823+2890 'Oranges' \
          source.jpg out.jpg

Composite Images

You can overlay one image over another by passing the image primitive to -draw:

$ convert -draw "image SrcOver 0 0 864 1059 source.jpg" \
          source.jpg out.jpg

References

Show Comments