Random circles

Generating random circles with ImageMagick

Basic script

I was cleaning my hard drive and I found this script I wrote a long time ago. It uses ImageMagick to draw a circle.

#!/bin/bash

convert -size 500x500 xc:black  \
        -draw "fill white circle 0,50,0,32  " circles.png

At some point I remember I was trying to draw random circles in squares images to test CellOrganizer. I wondered if there was a way to make this dumb script better.

Just a little better

By tweaking the script above a little we can change the location of the circle within the image. For example

#!/bin/bash

set -x
X=$((1 + $RANDOM % 500))
Y=$((1 + $RANDOM % 500))

convert -size 500x500 xc:black  \
        	    -draw "fill white translate $X,$Y circle 0,0,0,100  " circles.png

the script above generates a circle of radius 100 whose center is randomly generated.

However, running the script above generates a single image.

Wait.. there is more

With some tweaks to the script above we can generate more circles.

#!/bin/bash

set -x
for I in {1..25}
do
	X=$((1 + $RANDOM % 500))
	Y=$((1 + $RANDOM % 500))

	convert -size 500x500 xc:black  \
        	    -draw "fill white translate $X,$Y circle 0,0,0,100  " circles$I.png
done

montage -density 300 -tile 5x5 -geometry +5+5 -border 2 *.png montage.jpg
rm -f circles*.png

The script above will generate the following montage

Circles

What about the radius?

I guess you can use $RANDOM to generate random radii. Notice the script below is very similar to the previous, now I am just sampling the random radii.

#!/bin/bash

set -x
for I in {1..100}
do
	X=$((1 + $RANDOM % 500))
	Y=$((1 + $RANDOM % 500))
    RADIUS=$((1 + $RANDOM % 100))

	convert -size 500x500 xc:black  \
        	    -draw "fill white translate $X,$Y circle 0,0,0,$RADIUS" circles$I.png
done

montage -density 300 -tile 10x10 -geometry +5+5 -border 2 *.png montage10x10.jpg
rm -f circles*png

The script above will generate the following montage montage

Circles

But why?

Because I was bored. I was trying to remember why I built this, and I seemed to recall I built or explored this with Claire a long time ago.

At that time we were trying to generate synthetic images that could be used to test CellOrganizer and this was part of our exploration. Mostly because ImageMagick has a small footprint and a small learning curve.