Generative art in Python Processing

Lesson 16 of 20 · Python and Processing
Environment:
the examples in this lesson are written for Processing Python Mode. If you work in the recommended environment Thonny + py5, most of the code stays the same, and names with a capital letter in the middle are written in lower case with an underscore: mouseX → mouse_x, noStroke() → no_stroke(). See the table of differences.

Generative art is a way of creating images with code. Instead of drawing by hand, the program uses mathematics, random numbers and rules to generate art.

It is one of the most interesting uses of Python Processing and it is often used in creative projects, school papers and competitions.

If you have not done the basics of drawing, we recommend first:
Basic shapes (SR)
Animation and variables (SR)


Example 1: Random circles


def setup():
    size(600, 400)
    background(255)

    for i in range(100):
        x = random(width)
        y = random(height)
        r = random(10, 50)

        fill(random(255), random(255), random(255), 150)
        noStroke()
        ellipse(x, y, r, r)

The program draws 100 circles at random positions. Every circle has a different colour and transparency.


Example 2: Generative squares


def setup():
    size(600, 400)
    background(20)

    for x in range(0, width, 40):
        for y in range(0, height, 40):
            shade = random(255)
            fill(shade, 100, 255)
            rect(x, y, 30, 30)

Loops make it possible to create a grid of shapes. The colours are generated randomly.


Example 3: Animated generative art


def setup():
    size(600, 400)
    background(0)

def draw():
    x = random(width)
    y = random(height)
    fill(random(255), random(255), random(255), 100)
    noStroke()
    ellipse(x, y, 20, 20)

Every frame adds new shapes and creates an animated artistic effect.


Mini project: A dynamic collage

Make a generative collage with a combination of circles, squares and lines. You can add interaction with the mouse:


shapes = []

def setup():
    size(600, 400)
    background(0)

def draw():
    background(0, 10)  # a gentle trail effect

    # Adding new shapes by clicking
    if mousePressed:
        shapes.append((mouseX, mouseY, random(10,50), random(255), random(255), random(255)))

    for s in shapes:
        x, y, r, r_color, g_color, b_color = s
        fill(r_color, g_color, b_color, 150)
        noStroke()
        ellipse(x, y, r, r)

By clicking we add new shapes that stay on the screen. Animation and transparency create an interesting visual effect.


What we have learned

  • Generating random values with random()
  • Creating patterns with loops
  • Combining colours and transparency
  • Animation with draw()
  • Interactivity with the mouse
  • Creating a mini project with shapes that stay

Exercise

  • Make generative art using circles and squares
  • Add interactivity with the mouse (a click or the position)
  • Use transparent colours and animation
  • Make the colours of the shapes change depending on the x or y position

Frequently asked questions (FAQ)

What is generative art?

Creating visual content with code, where mathematical rules and random functions define the look.

Which Python Processing functions are used most often?

random(), ellipse(), rect(), line(), fill(), noStroke(), draw().

Do I need to know animation?

No, basic animation is easy to learn through draw() and variables for the position.

Can interactivity be used?

Yes, with the mouse or a click we can change the position, colour or size of the shapes.


Mini projects with generative art

This section gives ideas for small projects that you can implement right away in Python Processing. The goal is to combine randomness, loops and interactivity.

Project 1: Animated waves


def setup():
    size(600, 400)
    noFill()
    stroke(100, 150, 255)

def draw():
    background(0)

    for y in range(0, height, 20):
        beginShape()
        for x in range(0, width, 20):
            y_offset = sin((x + frameCount*5) * 0.05) * 20
            vertex(x, y + y_offset)
        endShape()

The waves move with animation. You can change the colour or the speed of the waves.

Project 2: Interactive spirals


def setup():
    size(600, 400)
    background(0)

def draw():
    translate(width/2, height/2)
    stroke(random(255), random(255), random(255))
    noFill()
    angle = frameCount * 0.1
    for r in range(0, 200, 5):
        x = r * cos(angle + r*0.1)
        y = r * sin(angle + r*0.1)
        ellipse(x, y, 5, 5)

def mousePressed():
    background(0)   # a click clears the screen

The spiral is animated, and the colours change randomly. With a click you reset the background.

Project 3: Clouds of shapes


shapes = []

def setup():
    size(600, 400)
    background(0)

def draw():
    background(0, 15)

    if mousePressed:
        for i in range(5):
            shapes.append((mouseX + random(-20,20),
                           mouseY + random(-20,20),
                           random(5,15),
                           random(255),
                           random(255),
                           random(255)))

    for s in shapes:
        x, y, r, r_c, g_c, b_c = s
        fill(r_c, g_c, b_c, 150)
        noStroke()
        ellipse(x, y, r, r)

By clicking you add "clouds" of shapes that stay on the screen and create a collage effect.

Practice tasks

  • Make your own mini generative project using a combination of shapes and colours
  • Add interaction with the mouse or the keyboard
  • Use animation through draw() and transparent colours
  • Experiment with random positions and sizes of shapes

Related lessons

From generative art to a competition paper

Check your knowledge

Open the quiz

Mini quiz: generative art

1. What does random(0, 255) give?




2. Why is background() often not drawn in every frame in generative art?




Useful external resources