An image, nevertheless, is not usually all one color. tint() is essentially the image equivalent of shape's fill(), setting the color and alpha transparency for displaying an image on screen. This type of simple image filtering is achieved with Processing's tint() function. Perhaps you would like the image to appear darker, transparent, blue-ish, etc. When displaying an image, you might like to alter its appearance. Optionally two arguments can be added to resize the image to a certain width and height. The image() function must include 3 arguments-the image to be displayed, the x location, and the y location. Once the image is loaded, it is displayed with the image() function. Loading images in draw() may result in slow performance as well as "Out of Memory" errors. We should also note that the process of loading the image from the hard drive into memory is a slow one, and we should make sure our program only has to do it once, in setup().
Create a blank image, 200 x 200 pixels with RGB color For creating a blank image, the createImage() function is used. We can think of it as the PImage constructor for loading images from a file. In fact, the loadImage() function performs the work of a constructor, returning a brand new instance of a PImage object generated from the specified filename. After all, in most object-related examples, a constructor is a must for producing an object instance. In the above example, it may seem a bit peculiar that we never called a "constructor" to instantiate the PImage object, saying new PImage(). Processing accepts the following file formats for images: GIF, JPG, TGA, PNG. Otherwise, place your image files inside. If there is no data directory create one. Images can be added to the data folder automatically via: loadImage() looks for image files stored in your Processing sketch's "data" folder. loadImage() takes one argument, a String indicating a file name, and loads the that file into memory. Second, a new instance of a PImage object is created via the loadImage() method. First, a variable of type PImage, named "img," is declared. Using an instance of a PImage object is no different than using a user-defined class. Draw the image to the screen at coordinate (0,0) Make a new instance of a PImage by loading an image file PImage img // Declare a variable of type PImage In this tutorial, we'll examine PImage, a class for loading and displaying an image as well as looking at its pixels.
In addition to user-defined objects (such as Ball), Processing has a bunch of handy classes all ready to go without us writing any code. Though perhaps a bit trickier, you hopefully also use objects, complex data types that store multiple pieces of data (along with functionality)-a "Ball" class, for example, might include floating point variables for location, size, and speed as well as methods to move, display itself, and so on. These are all primitive data types, bits sitting in the computer's memory ready for our use. You probably specify them often-a float variable "speed", an int "x", etc. Hopefully, you are comfortable with the idea of data types. This tutorial is dedicated to breaking out of simple shape drawing in Processing and using images (and their pixels) as the building blocks of Processing graphics. With a little creative thinking and some lower level manipulation of pixels with code, however, we can display that information in a myriad of ways. Most of the time, we view these pixels as miniature rectangles sandwiched together on a computer screen. If you see any errors or have comments, please let us know.Ī digital image is nothing more than data-numbers indicating variations of red, green, and blue at a particular location on a grid of pixels.
This tutorial is from the book Learning Processing by Daniel Shiffman, published by Morgan Kaufmann, © 2008 Elsevier Inc.