Basic shapes, colours and working with images in Processing (Python Mode)

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.
Lesson 5 of 20 · Python and Processing

A reminder: drawing shapes

In the previous lesson we learned how to draw basic shapes:

  • ellipse()
  • rect()
  • line()

Now we will learn how to control their colours and style, and then how to add images.

Note:

Processing uses the RGB colour model.
  • R — red
  • G — green
  • B — blue
Each value goes from 0 to 255.

The fill colour — fill()


def setup():
    size(600, 400)

def draw():
    background(255)
    fill(255, 0, 0)
    ellipse(300, 200, 120, 120)

The function fill(r, g, b) sets the colour of the inside of a shape.

Important: the colour stays active


fill(255, 0, 0)
ellipse(150, 200, 100, 100)

ellipse(450, 200, 100, 100)  # this one will be red too!
Change the colours and see the result:
  • fill(0, 255, 0)
  • fill(0, 0, 255)
  • fill(255, 255, 0)

The outline colour — stroke()


def setup():
    size(600, 400)

def draw():
    background(255)
    stroke(0, 0, 255)
    fill(255, 200, 0)
    rect(250, 150, 120, 100)

stroke() sets the colour of the outline.


Line thickness — strokeWeight()


strokeWeight(5)
line(50, 50, 550, 350)

noStroke() and noFill()


noStroke()
fill(0, 200, 150)
ellipse(300, 200, 150, 150)

noFill()
stroke(0)
rect(200, 150, 200, 100)

Working with images — loadImage() and image()

In real projects we use images for characters, backgrounds and objects.

Loading an image correctly


img = None

def setup():
    size(600, 400)

    global img
    img = loadImage("picture.png")

def draw():
    background(220)
    image(img, 200, 150)
Important:

The variable img must be global. If you define it only in setup(), it will not work in draw().

Changing the size of an image – img.resize()


img = None

def setup():
    size(600, 400)

    global img
    img = loadImage("picture.png")
    img.resize(150, 150)

def draw():
    background(220)
    image(img, 200, 150)

Several images


img1 = None
img2 = None

def setup():
    size(600, 400)

    global img1, img2
    img1 = loadImage("picture1.png")
    img2 = loadImage("picture2.png")

def draw():
    background(220)
    image(img1, 100, 150)
    image(img2, 350, 150)
A common mistake:

If you write:

def setup():
    img = loadImage("picture.png")
the image will not be shown because img is not global.

Where do you put the image?

The image must be in the data folder.
  • Sketch → Add File
  • or copy it into the data folder by hand

Experiment:

Try to:
  • move the image with mouseX
  • make a background
  • make a mini game
Adding images in Processing 4+: Add File automatically puts the image into the data folder
Picture 1: Adding images in Processing 4+. Adding with Add File automatically puts the image into the data folder

Positioning an image

The function image(img, x, y) sets where the image is shown on the screen.

  • x — the horizontal position (left → right)
  • y — the vertical position (top → bottom)
The coordinate (0, 0) is in the top left corner of the screen.

An image of a given size – image(img, x, y, width, height)


image(img, 100, 100, 200, 150)

Here we also set the width and the height of the image.

A note about dimensions:

  • an image can be smaller or bigger than the window
  • if it is bigger → part of the image will not be visible
  • if it is the same size as the window → it is used as a background

You can also use the original dimensions of the image:

img.width
img.height
A common mistake:

If the image is not shown:
  • check the file name (capital/small letters!)
  • check that it is in the data folder
  • check the format (png, jpg)
  • check that img is defined globally

Experiment:

  • Add your own image
  • Put it in the middle of the screen
  • Change its size
  • Stretch the image over the whole screen (a background)

Important: a global variable for the image

If you write:

def setup():
    img = loadImage("picture.png")
the image will not be shown in the draw() function.

✅ Correct:

img = None

def setup():
    global img
    size(600, 400)
    img = loadImage("picture.png")

def draw():
    background(220)
    image(img, 200, 150)

Why?
Because the variable must be available in the whole application (global scope).

How to explore on your own?

Programmers use documentation all the time.

Right-click on a function → Find in Reference

Or use the official documentation:
Processing Reference

  • explore image()
  • explore tint() (colouring an image)
  • explore resize()

Mini project: styled objects

Let us make a function that sets the style:


def set_style():
    fill(0, 150, 255)
    stroke(0)
    strokeWeight(3)

Then we use it:


def setup():
    size(600, 400)

def draw():
    background(240)

    set_style()
    ellipse(300, 200, 120, 120)
Why do we do this?

  • the code is clearer
  • we do not repeat the same lines
  • it is easier to change the style

Mini project: traffic lights

Challenge

Make traffic lights:
  • a red light
  • a yellow light
  • a green light
Extra:
  • use noStroke()
  • place them vertically
  • make a function for one light

Mini project: an image as a character


img = None

def setup():
    global img
    size(600, 400)
    img = loadImage("character.png")

def draw():
    background(200)
    image(img, 250, 150)

This is the basis for:

  • games
  • animations
  • interactive projects
Mini challenge:

  • centre the image
  • change its size
  • replace it with an image of your choice

Advanced challenge

Move the image using a variable:

x = 200

def draw():
    global x
    background(220)
    image(img, x, 150)
    x += 1

Comparison with the Java version

The same concepts exist in the Java version too:

Processing with Java — the basics


Next lesson

In the next lesson we learn animation:

Animation and the draw() function →


Related lessons


The same in Processing with Java

The logic is the same; only the syntax of the language is different.

Check your knowledge

Open the quiz

Mini quiz: colours

1. What does fill(255, 0, 0) do?




2. How do you switch off the outline of a shape?




3. Where must an image loaded by loadImage() be?