Basic shapes, colours and working with images in Processing (Python Mode)
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.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.
Processing uses the RGB colour model.
- R — red
- G — green
- B — blue
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!
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)
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)
If you write:
def setup():
img = loadImage("picture.png")
the image will not be shown because img is not global.
The image must be in the data folder.
- Sketch → Add File
- or copy it into the data folder by hand
Try to:
- move the image with mouseX
- make a background
- make a mini game
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)
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.
- 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
If the image is not shown:
- check the file name (capital/small letters!)
- check that it is in the
datafolder - check the format (png, jpg)
- check that
imgis defined globally
- 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
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)
- the code is clearer
- we do not repeat the same lines
- it is easier to change the style
Mini project: traffic lights
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
- centre the image
- change its size
- replace it with an image of your choice
Comparison with the Java version
The same concepts exist in the Java version too:
Processing with Java — the basics
Related lessons
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?
