Generative Art in Python

Max
Nerd For Tech
Published in
6 min readAug 6, 2021

--

Hello everybody! After a while of inactivity, I am back at writing! I was digging through my GitHub repositories and stumbled upon a bunch of cool (at least in my opinion) projects that I had made a while back during a hackathon. I decided to share a few of my GitHub easter eggs with you all, so without further ado, let’s check this one out!

What is Turtle (not the animal)?

Python

Turtle is a Python module which allows you to create pictures, draw shapes, and do anything artistic on a virtual canvas (which can have variable size). It is also used to create mini-games and fun animations. For example, you can play Hangman with it. The virtual pen/paintbrush is called the turtle, hence the name “Turtle.”

Great! Now let’s go and learn a bit about the type of art we (or the turtle I should say) will be doing, which is Generative Art.

A Brief Introduction to Generative Art

The Mandelbrot Fractal

Generative art is art that is generated. Ok. Not a very helpful definition, is it? More specifically, it is art that is built using code, and can include an autonomous system. The autonomous system we will use goes by the name of Randomness. Elements of randomness ensure that your output will be different (according to probability) every time you run the code, which I think is great because you’ll never know what you’re going to get. Of course, there are also less “random” autonomous systems, such as the Mandelbrot Fractal which is generated via a simple equation. For this project, I decided to balance chaos and order to create something which somewhat resembles the Mandelbrot Fractal.

The Project

Modules

The first step, as always, is to import our modules, which will be random and turtle. For this project, we don’t need to do any installs using pip, since the random and turtle modules are built in.

Importing Modules

Global Variables

The next step is to define some global variables, which will be used later on. The first is eps (for epsilon), a constant used in creating randomness, and the second is colours, which contains a list of colours we can draw in. Finally, we have scale, which will be a random number which defines how large our drawing will be.

Instantiating variables

Setup

We are almost ready to get into the body of the program. But first, we need to set up the turtle screen and initialize it. We can have any size canvas we want, but I chose 800 by 600 pixels so it fits nicely. Just choose any dimensions below your screen size.

Screen Setup

Now that the screen is ready, we can finally meet our turtle. I decided to call it melon for hackathon-related reasons. We’ll want the turtle to start somewhere near the bottom left of the screen, as the turtle will be moving mostly right and it can go off the screen.

Turtle!

Main Loop

Now we are finally into the main loop. This is where all the art drawing actually happens. We’ll want the program to loop a fairly large number of times, so I set that to a random number between 100 and 10,000 inclusive. The turtle will move in a zigzag right-left pattern while drawing almost-semi-circles (which are actually just polygons with a huge number of edges). Each iteration, the pen colour will bet set to a random colour so we can produce art that is colourful.

Quick summary:

  1. The turtle faces right.
  2. The turtle draws an arc of degree measure between 177 and 183 of a varying radius.
  3. The pen colour changes.
  4. The turtle draws another arc in the exact same manner as (2).
  5. The pen colour changes again.
  6. The turtle faces left and does steps 2–5
  7. Repeat between 100 and 10,000 times
Cool Stuff Happens HERE

If you want the art to be more organized, you can set scale to 5, and demon to 2. As well, setting the arc degree to 180 will produce a perfect semicircle, and setting the pen colour to a single colour will make the output less hypnotizing to look at.

Debugging

After running the program — hopefully it executed successfully — it should have produced (generated, I should say) art. If not, then it’s probably something wrong with Turtle. Check that whatever IDE you’re using is compatible with Turtle, and that everything is configured properly. An alternative solution is to use an online IDE such as Replit and make sure to select Python (with Turtle) when creating the project.

Like This.

Output:

After fixing all the bugs, the program should be working now. The output will vary, but here is an example of what our artistic turtle has produced for us. Looks pretty amazing!

Generative Art — made by Turtle

Mandelbrot Fractal

We can go a step further and try to generate the Mandelbrot Fractal. Another name for this is the Mandelbrot Set, which, when graphed in the complex plane, produces the fractal. Said set is defined by the set of complex numbers c for which the sequence Z remain bounded in absolute value. The sequence Z is defined by:

The modulus of a complex number is its distance to 0 in the complex plane. In Python, this is done using abs(z) where z is a complex number. We assume that the sequence Z is not bounded if the modulus of one of its terms is greater than 2.

Great! So how do we draw this thing?

A complex number (a+bi) can be represented on the complex plane. The complex plane is just the same as the Cartesian plane, but the y-axis is replaced with the imaginary axis which represents the value b in bi. The real part a is on the x-axis or the real axis.

We can draw the Mandelbrot Fractal as follows: For each point c of a part of the complex plane, we determine whether Z is bounded or not. The number of iterations needed to reach a modulus > 2 will determine what colour we use.

Steps

First, we import PIL, which will help us visualize the image we produce in great detail. Next, we define some variables which will help us calculate the terms of the sequence Z. As well, we define the size of the picture to be 768x768 and using RGB colour. After that, we loop 768 * 768 * (up to) 511 times to produce our output, with each iteration calculating the next term and plotting it in the given colour. By colour, I actually mean varying shades of black and white.

After we are done running our code, it should hopefully display and image of a colourful Mandelbrot Fractal. If not, try changing the last line to image.save('Mandelbrot.png', "PNG") , which will save the picture to your computer. It will have produced something like this:

Mandelbrot Fractal — made by You!

Conclusion

Congratulations! You have made it to the end of this article and what’s more, you’ve generated your own art created by Turtle, and the Mandelbrot Fractal as well! You’ve learned about Generative Art and the Mandelbrot Set, and programming in Python! You even have two (or more) artistic masterpieces which you can frame on your wall!

Next Steps

I encourage you to play around with the code and tweak it a bit. Try adding more colours, changing some variables and parameters, or running the code for more iterations, and see what outputs you get! Be creative!

More Coming Soon!

I am continuing to scrape through my GitHub repositories and I will post more “GitHub Hidden Gems” articles in the near future. In the meantime, stay tuned, and — Spoiler Alert! — see you next time, where we will creating a Music Visualizer. Click here if you want a sneak peak of what it looks like!

About Me

Hello everyone! I am human being who is passionate about technologies such as Quantum Computing, Blockchain, and Machine Learning/AI, as well as Space Technologies. Artistically useless, but musically useful; plays classical and jazz piano. I am currently working on a Quantum Convolutional Neural Network for Efficient Image Recognition using Tensorflow.

--

--

Max
Nerd For Tech

A person who is passionate about technologies such as Quantum Computing, Blockchain, and Machine Learning/AI. Currently working on a QCNN for Image Recognition!