# Digital Circlism: arrangement of circles over an image

**URL:** <https://community.wolfram.com/t/digital-circlism-arrangement-of-circles-over-an-image/17385>\
**Category:** Staff Picks\
**Tags:** image-processing, fun-and-play, computational-arts, geometry, graphics-and-visualization, staff-pick, wolfram-repositories\
**Created:** [January 11, 2021, 3:58pm UTC](https://community.wolfram.com/t/digital-circlism-arrangement-of-circles-over-an-image/17385 "2021-01-11T15:58:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![christophefavergeon](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/christophefavergeon/32/28845_2.png) [@christophefavergeon](https://community.wolfram.com/u/christophefavergeon)\
**Post date:** [January 11, 2021, 3:58pm UTC](https://community.wolfram.com/t/digital-circlism-arrangement-of-circles-over-an-image/17385/1 "2021-01-11T15:58:52Z")

</div>

_MODERATOR NOTE: this work is now a resource function:_  
[https://resources.wolframcloud.com/FunctionRepository/resources/DigitalCirclism/](https://resources.wolframcloud.com/FunctionRepository/resources/DigitalCirclism/)  
_full code and functions’ definitions can be found in the attached notebook at the end of the post._

* * *

## Introduction

Let’ s see how to generate pictures like:

 ![g5.jpeg](https://global.discourse-cdn.com/wolfram/original/3X/e/1/e12e1b17d2032593ea36c55342c8fd358ce3a8b3.jpeg)

Initially, I got the idea from this site:

[https://github.com/arihant-001/Circlism](https://github.com/arihant-001/Circlism)

But, I was not able to reproduce the results and my first algorithm was too slow.  
Then, I discovered this very interesting discussion:

[https://mathematica.stackexchange.com/questions/40334/generating-visually-pleasing-circle-packs](https://mathematica.stackexchange.com/questions/40334/generating-visually-pleasing-circle-packs)

But, it was not exactly like the first link.

After some experiments, and by mixing the ideas from the 2 previous links and some ideas of mine, I was able to generate the kind of pictures I wanted.

## Preprocessing

First, we need to extract the edges of the picture. I have found that the following steps are generally giving a good result.

I am resizing the image so that it is not too big. Otherwise the algorithm will be very slow. There are some optimizations I could do.

```
srcImage = 
 ColorConvert[
  ImageResize[
   RemoveAlphaChannel[ExampleData[{"TestImage", "Girl3"}], White], 
   400], "RGB"]

```

I am applying a MeanShiftFilter. But you could try with other processing to reduce the noise. There is no rule. It just has to look good at the end.

```
segmented = MeanShiftFilter[srcImage, 13, 0.1]

```

 ![g3.jpeg](https://global.discourse-cdn.com/wolfram/original/2X/c/c9b9171506b25320c69d88df2c94e97950368cf8.jpeg)

Finally, the edges are computed.

```
edges = ColorNegate@EdgeDetect[segmented]

```

 ![g4.jpeg](https://global.discourse-cdn.com/wolfram/original/2X/a/a5ba0d87574c9e106e228a9ef0f903b719ef215c.jpeg)

Now, the first key idea : the Euclidean Distance Transform will give the distance of a point to the closest edge. When you have this distance, then you can draw a circle which will not cross any edge.  
Since distances will be \> 1.0, you need an ImageAdjust if you want to display the result.  
I do it only as an illustration since this step is part of the main function.

```
DistanceTransform[edges] // ImageAdjust

```

 ![g10.jpeg](https://global.discourse-cdn.com/wolfram/original/3X/5/d/5ded828ee5e11565c1c4905239ba4c33631eed13.jpeg)

## Generation of the circles

The function below will try to place all the circles starting from the biggest to the smallest.  
The different scales to be considered are given by the list d.

```
d = {50, 20, 10, 5, 3, 2};

```

So, for instance, during the first pass, we will consider only the points with with an euclidean distance \> 50.0. This euclidean distance is the radius of the circle to place at this point.

We may not be able to place all the circles because of overlap with the circles already in place. So, when a circle cannot be placed, it is resized during the next phase.  
All circles with radius \> 50.0 will be resized to radius 50.0.  
If they still can’t get a place, then during the next phase they will be resized to 20 …

The overlap detection is the part to be optimized : I am comparing the circle to all of the already placed circles. Some partitioning of the plane may be useful to make this comparison quicker and avoid testing circles which are too far. In this version, it is the biggest limiting factor is you want to process big pictures.

```
result = circlism[d, edges];

```

The circlism function is returning a list of {position,radius}.

We use this list to draw the final pictures.

Two options are used by this function. “TimeConstraint” is set by default to 3 minutes. The image generation will stop after 3 minutes so you may not have all the circles.

“Pad” is set to 0.0 and controlling the padding between the circles.

## Picture Generation

Many variations are possible.

In this first picture, we just overlay the disks on top of the picture. The color of the disk is the color of the picture pixel at the center of the disk.

```
Show[srcImage, 
 Graphics[{EdgeForm[Thickness[0.001]], 
   MapThread[{imgColor[#1], Opacity[0.9], Disk[#1, #2]} &, result]}]]

```

 ![g5.jpeg](https://global.discourse-cdn.com/wolfram/original/3X/e/1/e12e1b17d2032593ea36c55342c8fd358ce3a8b3.jpeg)

In this variation, we use an uniform background. I am generating this background with an ImageApply because I have observed that the Show is clipping the Graphics as it should when an image is used.

```
Show[uniformBackground[0.8], 
 Graphics[{EdgeForm[], 
   MapThread[{imgColor[#1], Disk[#1, #2]} &, result]}]]

```

 ![g6.jpeg](https://global.discourse-cdn.com/wolfram/original/3X/c/c/cc8ba5b9a8f319c29df97af94f6f56127c250201.jpeg)

In this other variation, we use the colFunc function which is using a background mask. For circles in the background, a random color from a palette is used.

```
Show[srcImage, 
 Graphics[{EdgeForm[Thin], 
   MapThread[{colFunc["Aquamarine", #1], Disk[#1, #2]} &, result]}]]

```

 ![g7.jpeg](https://global.discourse-cdn.com/wolfram/original/3X/2/f/2fee3feb16fbf797a8822538e5181e114e4fd278.jpeg)

Why only use circles ?

```
Show[srcImage, 
 Graphics[{EdgeForm[Thin], 
   MapThread[{colFunc["Aquamarine", #1], RegularPolygon[#1, #2, 6]} &,
     result]}]]

```

 ![g9.jpeg](https://global.discourse-cdn.com/wolfram/original/3X/9/e/9e6d347069c78521b97a6430a4d6659e700284d6.jpeg)

[Circlism.nb](https://community.wolfram.com/uploads/short-url/fwgb4PYjaZ85zRnVmasGoEGWfn8.nb) (7 MB)

---

<div class="post-metadata">

**Author:** ![wolframcommunity](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wolframcommunity/32/8335_2.png) [@wolframcommunity](https://community.wolfram.com/u/wolframcommunity)\
**Post date:** [January 11, 2021, 7:37pm UTC](https://community.wolfram.com/t/digital-circlism-arrangement-of-circles-over-an-image/17385/2 "2021-01-11T19:37:13Z")

</div>

![news-congrads-kkluyshnik-02-04-19.jpg](https://global.discourse-cdn.com/wolfram/original/3X/6/a/6a310c2d0904616faf4e6e148260c9f29ab9f4eb.jpeg) – you have earned _ **Featured Contributor Badge** _ ![2015-11-02_10-07-11.png](https://global.discourse-cdn.com/wolfram/original/3X/5/0/50e59c9ead62f729e7130d3ea1482555a75602b6.png)  
Your exceptional post has been selected for our editorial column _ **Staff Picks** _ [http://wolfr.am/StaffPicks](http://wolfr.am/StaffPicks) and [Your Profile](http://community.wolfram.com/web/christophefavergeon) is now distinguished by a _ **Featured Contributor Badge** _ and is displayed on the [Featured Contributor Board](https://community.wolfram.com/people). Thank you!

---

<div class="post-metadata">

**Author:** ![christophefavergeon](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/christophefavergeon/32/28845_2.png) [@christophefavergeon](https://community.wolfram.com/u/christophefavergeon)\
**Post date:** [January 11, 2021, 8:02pm UTC](https://community.wolfram.com/t/digital-circlism-arrangement-of-circles-over-an-image/17385/3 "2021-01-11T20:02:38Z")

</div>

Thank you.

---

<div class="post-metadata">

**Author:** ![jonm](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/jonm/32/38631_2.png) [@jonm](https://community.wolfram.com/u/jonm)\
**Post date:** [January 13, 2021, 2:13pm UTC](https://community.wolfram.com/t/digital-circlism-arrangement-of-circles-over-an-image/17385/4 "2021-01-13T14:13:09Z")

</div>

I took a similar approach with TessellateGraphics:

[https://resources.wolframcloud.com/FunctionRepository/resources/TessellateGraphics](https://resources.wolframcloud.com/FunctionRepository/resources/TessellateGraphics)

though I never thought of using EdgeDetect to apply it images that were not already masks.

[  
  ![ScreenShot2021-01-13at9.13.18AM.jpg](https://global.discourse-cdn.com/wolfram/original/2X/6/696b711e8066aefb7653a83e80b460dad78aafd7.jpeg)  
 ](https://resources.wolframcloud.com/FunctionRepository/resources/TessellateGraphics)

---

<div class="post-metadata">

**Author:** ![christophefavergeon](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/christophefavergeon/32/28845_2.png) [@christophefavergeon](https://community.wolfram.com/u/christophefavergeon)\
**Post date:** [January 13, 2021, 2:56pm UTC](https://community.wolfram.com/t/digital-circlism-arrangement-of-circles-over-an-image/17385/5 "2021-01-13T14:56:28Z")

</div>

Great. I’ll play with your function.

And perhaps I’ll push this work to the function repository when I have learnt how to do it.

---

<div class="post-metadata">

**Author:** ![jonm](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/jonm/32/38631_2.png) [@jonm](https://community.wolfram.com/u/jonm)\
**Post date:** [January 13, 2021, 4:09pm UTC](https://community.wolfram.com/t/digital-circlism-arrangement-of-circles-over-an-image/17385/6 "2021-01-13T16:09:41Z")

</div>

Its pretty easy to submit items. Use the menu New/RepositoryItem/Function Repostory and just fill in the notebook.

If you can see an syntax extension that don’t create backward compatibility, feel free to extend the source code of mine by editing  
[https://www.wolframcloud.com/files/4793f5f4-937f-49ff-852b-633e518e2b5c?contentDisposition=attachment](https://www.wolframcloud.com/files/4793f5f4-937f-49ff-852b-633e518e2b5c?contentDisposition=attachment)

---

<div class="post-metadata">

**Author:** ![christophefavergeon](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/christophefavergeon/32/28845_2.png) [@christophefavergeon](https://community.wolfram.com/u/christophefavergeon)\
**Post date:** [February 4, 2021, 4:49pm UTC](https://community.wolfram.com/t/digital-circlism-arrangement-of-circles-over-an-image/17385/7 "2021-02-04T16:49:31Z")

</div>

And it is finally a [resource function](https://resources.wolframcloud.com/FunctionRepository/resources/DigitalCirclism/) !

---

<div class="post-metadata">

**Author:** ![jonm](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/jonm/32/38631_2.png) [@jonm](https://community.wolfram.com/u/jonm)\
**Post date:** [February 4, 2021, 4:54pm UTC](https://community.wolfram.com/t/digital-circlism-arrangement-of-circles-over-an-image/17385/8 "2021-02-04T16:54:29Z")

</div>

Nice

---

<div class="post-metadata">

**Author:** ![wolframcommunity](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wolframcommunity/32/8335_2.png) [@wolframcommunity](https://community.wolfram.com/u/wolframcommunity)\
**Post date:** [February 26, 2021, 2:26pm UTC](https://community.wolfram.com/t/digital-circlism-arrangement-of-circles-over-an-image/17385/9 "2021-02-26T14:26:59Z")

</div>

Congratulations! Your post was highlighted on the Wolfram’s official social media channels. Thank you for your contribution. We are looking forward to your future posts.

- [Twitter](https://twitter.com/WolframResearch/status/1353765022590230532)

- [Facebook](https://www.facebook.com/wolframresearch/posts/10158227416603795)

- [Instagram](https://www.instagram.com/p/CKeis_LjjrT/)

---

<div class="post-metadata">

**Author:** ![christophefavergeon](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/christophefavergeon/32/28845_2.png) [@christophefavergeon](https://community.wolfram.com/u/christophefavergeon)\
**Post date:** [July 8, 2022, 3:42pm UTC](https://community.wolfram.com/t/digital-circlism-arrangement-of-circles-over-an-image/17385/10 "2022-07-08T15:42:44Z")

</div>

The algorithm is working well on video too:

[![](https://global.discourse-cdn.com/wolfram/original/3X/7/3/730b2b5d417fc0e7b2d5157e94f4d749da330741.jpeg "Circles") ](https://vimeo.com/629924483)

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/587f8a95-7201-4107-a13e-3f7b05f0b92c)

---

<div class="post-metadata">

**Author:** ![Vitaliyk](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/vitaliyk/32/45591_2.png) [@Vitaliyk](https://community.wolfram.com/u/Vitaliyk)\
**Post date:** [July 8, 2022, 8:18pm UTC](https://community.wolfram.com/t/digital-circlism-arrangement-of-circles-over-an-image/17385/11 "2022-07-08T20:18:12Z")

</div>

Very nice video! Is there a code for it? I wonder if it is possible to make an animated GIF.

---

<div class="post-metadata">

**Author:** ![christophefavergeon](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/christophefavergeon/32/28845_2.png) [@christophefavergeon](https://community.wolfram.com/u/christophefavergeon)\
**Post date:** [July 9, 2022, 5:15am UTC](https://community.wolfram.com/t/digital-circlism-arrangement-of-circles-over-an-image/17385/12 "2022-07-09T05:15:31Z")

</div>

Here is a notebook. The code can be easily modified to change the style of the result.

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/49bf90a3-9307-429f-9e71-e617c9a58996)
