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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

432<br />

Image manipulation<br />

Figure 10-22. Color Static sketch<br />

As you might suspect, the pixels[] array has better uses than creating static.<br />

loadPixels() collects all the pixels in the window, regardless of whether the pixels make<br />

up a single color background fill or a photographic image. The more interesting latter case<br />

opens the door to all kinds of imaging possibilities. However, before looking at the next<br />

sketch, let’s review how to load an image.<br />

To work with images in <strong>Processing</strong>, you need to load them into a data directory that<br />

resides within the current sketch directory. It’s a very simple process. Under Sketch in the<br />

top menu bar, select the Add File comm<strong>and</strong>. This comm<strong>and</strong> opens a file browser, allowing<br />

you to select an image to copy into the data directory. If there isn’t a data directory,<br />

<strong>Processing</strong> will automatically create one for you. If you eventually make additional changes<br />

to the image using a program like Photoshop, you’ll need to remember to add the altered<br />

image to your data directory again (it’s not updated automatically). One cautionary note:<br />

when you add an image into the data directory with the same name as an existing image,<br />

the original image will be replaced by the image you add, without any warning. <strong>Processing</strong><br />

can work with GIF, JPG, TGA, <strong>and</strong> PNG images.<br />

The following sketch (shown in Figure 10-23) loads an image (my dog Heidi) into the display<br />

window (obviously, you’ll need to change the name heidi.jpg to the image you’re<br />

using):<br />

// Load an Image<br />

size(600, 400);<br />

PImage img1;<br />

img1 = loadImage("heidi.jpg");<br />

image(img1, 0, 0);

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

Saved successfully!

Ooh no, something went wrong!