Reference for Processing version 1.2. If you have a previous version, use the reference included with your software. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Javadoc.

Name

loadImage()

Examples
example pic
PImage b;
b = loadImage("laDefense.jpg");
image(b, 0, 0);
example pic
// Loading an image when using setup() and draw()

PImage b;

void setup() {
  b = loadImage("laDefense.jpg");
  noLoop();
}

void draw() {
  image(b, 0, 0);
}
PImage online;

void setup() {
  String url = "http://processing.org/img/processing_beta_cover.gif";
  online = loadImage(url, "png");
  noLoop();
}

void draw() {
  image(online, 0, 0);
}
Description Loads an image into a variable of type PImage. Four types of images ( .gif, .jpg, .tga, .png) images may be loaded. To load correctly, images must be located in the data directory of the current sketch. In most cases, load all images in setup() to preload them at the start of the program. Loading images inside draw() will reduce the speed of a program.

The filename parameter can also be a URL to a file found online. For security reasons, a Processing sketch found online can only download files from the same server from which it came. Getting around this restriction requires a signed applet.

The extension parameter is used to determine the image type in cases where the image filename does not end with a proper extension. Specify the extension as the second parameter to loadImage(), as shown in the third example on this page.

If an image is not loaded successfully, the null value is returned and an error message will be printed to the console. The error message does not halt the program, however the null value may cause a NullPointerException if your code does not check whether the value returned from loadImage() is null.

Depending on the type of error, a PImage object may still be returned, but the width and height of the image will be set to -1. This happens if bad image data is returned or cannot be decoded properly. Sometimes this happens with image URLs that produce a 403 error or that redirect to a password prompt, because loadImage() will attempt to interpret the HTML as image data.
Syntax
loadImage(filename)
loadImage(filename, extension)
Parameters
filename String: name of file to load, can be .gif, .jpg, .tga, or a handful of other image types depending on your platform.
extension String: the type of image to load, for example "png", "gif", "jpg"
Returns PImage or null
Usage Web & Application
Related PImage
image()
imageMode()
background()
Updated on June 14, 2010 12:05:29pm EDT

Creative Commons License