Coder Social home page Coder Social logo

gaw1ik / generative-art-in-python-close-packed-circles Goto Github PK

View Code? Open in Web Editor NEW
12.0 1.0 2.0 3.3 MB

This repo details an algorithm for creating images containing closely packed circles that don't overlap. Generative art, code art, geometric art.

Python 100.00%
generative-art code-art geometric-algorithms geometric-art

generative-art-in-python-close-packed-circles's Introduction

Generative Art In Python: Close-Packed Circles

I've been creating a lot of "generative art" recently (mainly using Python), and lately I've wanted to be able to generate circles - potentially hundreds of them at random - which would not overlap with each other. This seems like a trivial problem at first, but like most programming problems that initially seem trivial, this was actually kind of a tricky problem to solve. My solution is documented here.

If you want to follow my art (and also gawk at some old food pics) you can check me out on instagram @brian_gawlik.

Description of Algorithm

The GIFs above provide a visual for how the algorithm works, and the still images show some examples of the intended artistic output. First, an initial circle is placed at random with radius equal to the biggest desirable radius (specified in the Inputs section). Then one-by-one, additional circles are placed at random in the remaining available space in the frame (areas not yet covered by a circle). An image mask can be used here to dictate the available space, confining the circles to arbitrary regions (as is done in the static png examples above). These circles are initially given the smallest desirable radius (also specified in the Inputs), and then their radius is increased gradually, until they "bump" into any existing circles (the bump condition), at which point the image created in the previous iteration is recorded and the process is repeated until the stop condition is met.

The two conditions are defined as follows:

The "Bump" Condition:

The "bump" condition is defined as when (nRegions < nCircles).

Where:

nRegions = # of image regions (AKA segments) as calculated using skimage.measure.regionprops

nCircles = # of circles currently placed in the frame which are tallied as the code iterates

When the circles are not overlapping, nRegions should be equal to nCircles, because each region is a circle, but once there is an overlap, nRegions will be less than nCircles, because at that point, multiple circles are contributing to the same region.

The Stop Condition:

The number of failed attempts to create a new circle are tallied during each iteration. Failed attempts happen when the initial placement of a circle overlaps with existing circles. When the number of failed attempts exceeds a certain number (I have used anywhere from 150 to 1000 for these examples) the program is ended. This is a fairly non-robust stop condition, but it gets the job done just fine (at least for this range of Input parameters).

Points of Discussion

  1. The code provided in this repository generates a mask (binary image) containing the closely packed circles. It should plot after being run. The script has a few inputs which allow you to adjust the size of the mask (width and height), the stop condition, the biggest and smallest desirable radii, and the seed for the random arrangement.

  2. In the still images shown above, I have used the final output (the image mask containing the packed circles) as an input to another code which uses the pycairo library to draw circles of different colors. Pycairo is a graphics library for python which enables convenient illustration tools such as coloring, outlining, etc. Unlike the Skimage library, which I use for the initial mask creation, Cairo draws circles with interpolated edges. This avoids jagged edges, which are especially prevalent in smaller circles when using Skimage, and the smallest circles literally just become squares :( I digress... The location and size of the circles are dictated by the output mask based on the centroid and radius (sqrt(area/pi)) of the mask's regions (calculated using skimage.measure.regionprops). Also, in the generation of the packed circles mask, a separate mask containing one large center circle is used to constrain the drawing area, resulting in the packed circles forming a larger circle of their own... Cool!

Brief Closing Thoughts

This certainly isn't the most robust approach to this problem, and yet, I like the artistic nature of this solution. Something about it feels very organic, almost as if the algorithm is really trying to make itself work. The random selection and trial and error of the process makes for a great visual as well.

generative-art-in-python-close-packed-circles's People

Contributors

gaw1ik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.