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.

Loading & Displaying<br />

The Loading & Displaying section includes two functions that make working with images in<br />

<strong>Processing</strong> a joy: loadImage() <strong>and</strong> image(). loadImage("imageName") expects a single<br />

String argument, which should be the name of the file you want to load, residing in the<br />

sketch’s data directory. It is critical that you first load your image files into <strong>Processing</strong> by<br />

selecting Add File from the Sketch menu. When you select Add File, your loaded image will<br />

be placed in a data subdirectory, within the current sketch main directory. If you don’t<br />

already have a data directory, it will be created for you. If you’ve successfully run the last<br />

few examples, you already know how to do this.<br />

It is recommended that you do all your image loading in the setup() structure, not in<br />

draw(). After you load an image using loadImage(), you won’t see it until you render it to<br />

the screen. This is another one of those things that’s not so simple to do in Java, but<br />

incredibly simple to achieve in <strong>Processing</strong>—just call image(). This function can take a<br />

bunch of arguments, including a PImage object reference <strong>and</strong> the x, y, width, <strong>and</strong> height<br />

properties of the image.<br />

This subsection also includes basic tinting functions <strong>and</strong> a simple utility function called<br />

imageMode() that gives you two options for specifying image measurements in the display<br />

window when you call the image() function. The default CORNER argument option allows<br />

you to specify x, y, width, <strong>and</strong> height properties, while the CORNERS argument makes the<br />

third <strong>and</strong> fourth measurement arguments (that were width <strong>and</strong> height) specify the position<br />

of the bottom-right point of the image. In both cases, when passing arguments that<br />

control width <strong>and</strong> height, you can scale <strong>and</strong> load the images simultaneously. Be aware that<br />

you can scale the images both proportionally <strong>and</strong> non-proportionally based on the ratio of<br />

your arguments.<br />

As an example, I created a simple progressive tiling sketch that loads an image, <strong>and</strong> then<br />

continuously shrinks the image as it tiles it through the display window (see Figure A-17).<br />

Be aware that there is the possibility of generating an infinite loop with this example—one<br />

of Zeno’s paradoxes, in which a float value keeps approaching a discrete value, but never<br />

reaches it. You can read about Zeno’s paradoxes at http://en.wikipedia.org/wiki/<br />

Zeno%27s_paradox. If you test this, please remember to first add your image file into the<br />

data directory using the Add File comm<strong>and</strong>. (This sketch uses the image vail.jpg.)<br />

/*<br />

Progressive Tile<br />

Ira Greenberg, November 12, 2005<br />

*/<br />

size(480, 480);<br />

background(255);<br />

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

float x = 0, y =0;<br />

float w = 240*.5;<br />

float h = w;<br />

for (int i=0; i

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

Saved successfully!

Ooh no, something went wrong!