The Eclectic Quill

Website of Joshua McGee


Measuring the diameter of a circle in ImageMagick

If you have an image (or crop of an image) that contains just a circle against a solid background of a contrasting color, you can measure its diameter with the ImageMagick command line tools.

Let's generate an image to test it:

convert -size 400x400 xc:skyblue -fill white -stroke black -draw "circle 260,220 280,340" circle.png

That generates this image:

What are the dimensions of that circle?  Run this command:

convert circle.png +repage -fuzz 5% -trim +repage - | identify -format '# %wx%h\n' -

You get this output:

# 245x245

If the numbers on the sides of the x are not within 1px of one another, change the value of fuzz, marked in blue in the code above.  If it is a lossless image format such as PNG or GIF, make it 0%.  If it's a JPEG, try increasing it.  Once they are within one pixel, you have identified the circle's diameter: in this case, 245 pixels.

That is because, internally, you have created this trimmed image:

Leave a Reply

Your email address will not be published. Required fields are marked *