04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Image<br />

The Image section relates very directly to the information I covered in the Color section, as<br />

an image after all is a just a collection of colored pixels. In general, the image functions<br />

allow you to load <strong>and</strong> alter images. In Java, loading images <strong>and</strong> altering them is real work<br />

(especially down at the pixel level). <strong>Processing</strong>, as usual, simplifies this entire process, while<br />

still giving you enough low-level access to act directly upon the pixel data. <strong>Processing</strong> also<br />

includes its own image data type, called PImage, that encapsulates many of the features,<br />

which makes working with images in <strong>Processing</strong> so easy. Yet, for all its ease of use,<br />

<strong>Processing</strong>’s imaging capabilities are surprisingly powerful.<br />

The Image section of the API is divided up into essentially two categories, with the PImage<br />

data type <strong>and</strong> createImage() function st<strong>and</strong>ing alone. The two categories are Loading &<br />

Displaying <strong>and</strong> Pixels. Be aware that PImage includes a bunch of methods with the same<br />

names as some of the functions in the Pixels section. For instance, following is an example<br />

that uses the PImage method get() <strong>and</strong> the function get() in the same sketch. Even<br />

though these comm<strong>and</strong>s are both called get(), they are fundamentally different. The<br />

get() method requires a PImage object to call it—as in myImage.get()—while the get()<br />

function works all by itself. The subtlety of this distinction requires an underst<strong>and</strong>ing of<br />

OOP, which is covered in Chapter 8.<br />

You’ll need an image named portrait.jpg in the data directory of this sketch to run this<br />

example. I suggest making the image 400 by 400 pixels.<br />

size(400, 400);<br />

PImage img = loadImage("portrait.jpg");<br />

image(img, 0, 0);<br />

println("image method before = " + img.get(50, 50));<br />

println("function before = " + get(50, 50));<br />

image(img, -140, -140);<br />

println("image method after = " + img.get(50, 50));<br />

println("function after = " + get(50, 50));<br />

Here’s the output generated by running the sketch:<br />

image method before = -263171<br />

function before = -263171<br />

image method after = -263171<br />

function after = -5686717<br />

Your values will be different, since your image will be different than mine—but notice how<br />

the output changes for the function, but not for the method. What’s happening is that the<br />

PImage get() method references the image data stored in the PImage object independently<br />

from its display on the screen, while the get() function returns pixel values at a specific<br />

point on the screen. Since I drew the image at two different places on the screen, the<br />

two get() function calls give two different values. However, the two get() method calls<br />

refer to the same pixel in the stored PImage object.<br />

PROCESSING LANGUAGE API<br />

731<br />

A

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!