ART WITH PYTHON
Artistic Voronoi Diagrams in Python
Look mama I am an artist
Purpose: We will learn how to make a Voronoi diagram with Python.
Note: In the case you’re starting from scratch, I will advise you to follow this article and install all the necessary libraries. Finally, to get the most of this article view it on a PC or Mac with a large monitor so that you can see the details of the image.
Hardware: My workstation equipped with Inter(R)Core(TM) i7–8700 with 12 CPU @ 3.70 Ghz and NVIDIA GeForce RTX 2080.
A Word From the Author
So the other day while I was browsing the Data is Beautiful subreddit, I came across a beautiful looking Voronoi diagram. I decided that I wanted to recreate the diagram so that I could make long strips of it and used them on my Medium articles to separate sections. I could try to explain to you what Voronoi diagram is but this 3 minute video does such amazing job you should go and watch it:
With that said let’s create some stunning visuals.
Making a Voronoi Diagram
Let’s start by importing all the libraries we need.
If you get any ModuleNotFoundError
let’s take care of them using Anaconda to install the missing packages. I will only go over the less common packages, since it’s likely you already have Pandas, Matplotlib, and other widely used packages installed.
Open Anaconda Prompt and navigate to the desired conda environment.
To install PIL a Python package used for image processing:
conda install -c anaconda pillow
Now let’s define the helper function that will do the bulk of the work:
You can read the doc string to understand the parameters the function takes. Click here to see some of the color palettes you can use.
To create the Voronoi diagram, sites are randomly drawn from a 2D Gaussian distribution. Then, each site is assigned a random color from one of the two palettes. Finally, the diagram is created pixel by pixel and then saved.
Now we are ready to make a cool looking diagram.
I would like to note that Script 3 would take a long time to run for large images and if you use a large number of cells / sites.
Closing Remarks
If you improve this or make cooler diagrams please share them with me. I would love to see the results.
You’re welcome to connect with me on LinkedIn. Until next time and code every day!