Tutorial: Images

e.g., mouse, keyboard
e.g., visualization, fractal, mouse
Learn more about Creative Commons
Join Plus+ to change privacy settings
  • This is a tutorial on how to load and display images in p5.


    Since we are working in the online editor of OpenProcessing, we need to upload our images before we can access them in our program. To do this we click at the three dots on the right, then on "Files" and then we choose our image via the "select" window or by directly dropping the file there.


  • Now we create our global image variable "panda".

  • As you already know the setup() function is called before our draw() loop starts. But Javascript is asynchronous, meaning that it allows multiple things to happen at the same time. So if we would load our image in setup(), it could happen that the image file is still not fully loaded before it's needed in another line of code. Therefore it's better to use another built-in function of p5: preload().

  • The loadImage command loads an image from the given path. Because we uploaded our file to this sketch in OpenProcessing, we can just use the filename "panda.jpg". We could also put in a URL to an image file here.

    When we run our sketch, p5 calls preload() first, and then pauses execution until loadImage has finished loading our image file.

  • Now we display the image in our draw() function with the image command:



    As input parameters we use:

    1. Image variable: panda
    2. Position on the x axis (upper-left corner): 50
    3. Position on the y axis (upper-left corner): 100
    4. Optional: width of the displayed image
    5. Optional: height of the displayed image


    Note that the reference point for positioning the images is the upper-left corner (for ellipses it is the center).


    Since we leave out width and height, the image is displayed in its full dimensions.

1/5
  • mySketch
  • v1.11.3
  • v1.11.2
  • v1.11.1
  • v1.11.0
  • v1.10.0
  • v1.9.4
  • v1.9.3
  • v1.9.2
  • v1.9.1
  • v1.9.0
  • v1.8.0
  • v1.7.0
  • v1.6.0
  • v1.5.0
  • v1.4.2
  • v1.4.1
  • v1.4.0
  • v1.3.1
  • v1.3.0
  • v1.2.0
  • v1.1.9
  • v1.1.7
  • v1.1.5
  • v1.1.4
  • v1.1.3
  • v1.0.0
  • v0.10.2
  • v0.9.0
  • v0.8.0
  • v0.7.3
  • v0.7.2
  • v0.6.1
  • v0.6.0
  • v0.5.16
  • v0.5.11
  • v0.5.8
  • v0.5.7
  • v0.5.2
Select mode or a template
Centers sketch and matches the background color.
Prevents infinite loops that may freeze the sketch.
This will be the default layout for your sketches
Easy on the eyes
It will show up when there is an error or print() in code
Potential warnings will be displayed as you type
Closes parenthesis-like characters automatically as you type

Controls
Play
Ctrl+Enter
Code
Ctrl+Shift+Enter
Save
Ctrl+S
Interface
Fullscreen
Ctrl+Alt+F
Switch Layout
Ctrl+Alt+L
Settings
Ctrl+Alt+.
Editor
Tidy Code
Ctrl+B
Multiple Cursors
Ctrl+Click
Duplicate Line/Selection
Ctrl+Shift+D
Move Line
Alt+↑/↓
Select Multiple
Ctrl+D
Find in Code
Ctrl+F
Find Next
Ctrl+G
Find Previous
Ctrl+Shift+G

See More Shortcuts

Join Plus+ for private sketches, version history, 1GB space, custom embeds, and more!