Saint Louis University |
Computer Science 1050
|
Dept. of Math & Computer Science |
PImage data type
A PImage object is the equivalent of an "off-stage" canvas that stores an array of pixels. We can examine and manipulate the color settings for those pixels and later render the PImage to an actual canvas.
loadImage() function
Examples:
PImage slu = loadImage("campus4.jpg"); // must be stored in sketch directory
PImage slu = loadImage("/Users/goldwasser/Documents/campus4.jpg");
PImage slu = loadImage("http://business.slu.edu/uploads/2012/11/08/slu-homepage-campus4.jpg");
createImage() function
We can also create a new (blank) PImage object by using the createImage() function with a given width and height and color model. Examples are:
PImage example = createImage(400,200,RGB);To create a new 400-by-200 image using RGB colors (but no alpha channel)
PImage sample = createImage(400,200,ARGB);To create a new 400-by-200 image using RGB colors with alpha
PImage sample = createImage(400,200,ALPHA);To create a new 400-by-200 image using grayscale color with alpha
background() function
We have previously used the background function to set the entire canvas background to a particular color. It is also possible to set the entire canvas background to a given image, as in
background(slu);although this only works if the canvas has width and height that precisely match the image.
image() function
We can display a particular PImage object on our canvas, with top-left corner at (x,y) using a command of the form:
image(slu, x, y);
You can also use transformation functions such as scale and rotate to alter the canvas coordinate system before rendering an image.
Useful functions of a PImage object
For the following examples, assume our image is named with variable img.
get(x, y);Returns the color of the pixel at coordinate (x,y) of the PImage.
color c = img.get(25,0);
get(x, y, w, h);Returns a new PImage of width w and heighth which is a copy of the rectangle anchored at (x,y) in the original PImage.
PImage sample = img.get(25,0,100,50);
set(x, y, c);Set the pixel at coordinate (x,y) of the PImage to color c.
img.set(100, 50, color(255,0,0));
set(x, y, image);Sets the rectangle anchored at coordinate (x,y) in one PImage to be a copy of an entire other image.
img.set(100, 50, slu);
Additional "Bells and Whistles"
There exists functionality for many common image processing tasks
(although, for the purpose of exploration, we may later develop our
own low-level implementations for many of these). To demonstrate
several of these, we rely on the following reference image.
img.save(filename);Saves the current PImage directly to a file (typically .jpg, .tif, .png format).
img.resize(w,h);Resizes an image to new width and height (scaling appropriately).
img.mask(other);Uses the other PImage as a "mask" for the first image. The other image should use grayscale values, but those grayscale values will be treated as new alpha values for transparency in masking the first image.
img.filter(...);has many uses:
img.filter(GRAY);Converts image to grayscale.
img.filter(INVERT);Converts image to "negative" color, in effect inverting the color components for each pixel
img.filter(THRESHOLD, pct);Converts image to black and white, depending on whether each pixel is above or below the given threshold percentage.
img.filter(BLUR, radius);Blurs the image using a Gaussian blur with the given radius.
img.filter(ERODE);Reduces the light areas.
img.filter(DILATE);Increases the light areas.
img.filter(POSTERIZE, num);For each color channel, reduces the number of distinct color values that can be used to num (for some
img.blend(...);blending is another multipurpose tool that allows you to compose two images (or portions thereof) on a pixel-by-pixel basis. Depending on the parameterization, it can take a (weighted) average of the two corresponding pixels, add them, subtract them, take the darker or lighter of the two, and various other forms of combination.
See documentation for more details.