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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

440<br />

Display window functions<br />

The comm<strong>and</strong>s grouped under the Pixels heading of the language reference are functions<br />

that affect the pixels in the display window, regardless of whether those pixels make up a<br />

background color or an image. The pixels[] array specified under this heading is the<br />

array that holds all the pixels making up the display window (again, regardless of what’s<br />

in it).<br />

PImage methods<br />

The PImage data type also has a pixels[] array property that is assigned the pixels making<br />

up the created PImage object. When invoking a method of the PImage class, you use a<br />

PImage object reference attached to the property or method with dot syntax. For example,<br />

to set a pixel of a PImage named img, you would write img.set(). In contrast, to set a pixel<br />

in the display window, you would just use set(). The situation can seem even more confusing<br />

once img (the PImage object) is drawn to the screen. At that point, the image data<br />

becomes part of the display window, which gets collected in the window’s pixels[] array.<br />

However, img still retains its original pixel data in memory (in its own img.pixels[] array).<br />

The next sketch, shown in Figure 10-28, uses the PImage method get() to create a pixilated<br />

image. The variable detail allows you to change the level of pixilation. The original<br />

PImage is never drawn to the screen, but rather the internal image is used for picking color<br />

values with the PImage get() method. The picked color values are then used to create a<br />

matrix of colored rectangles approximating the image. If you bring the level of detail down<br />

to (or near) 1, the colored rectangles will approximate a continuous tone image; you’ll also<br />

want to uncomment the line //noStroke();. I recommend using an image the same size<br />

as the display window.<br />

//Pixilate<br />

size(600, 400);<br />

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

//noStroke();<br />

int detail = 10;<br />

for ( int i=0; i

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

Saved successfully!

Ooh no, something went wrong!