1 of 15

CHAPTER 15: IMAGES

SECTIONS 1, 2, 3

2 of 15

GETTING STARTED

4 steps to draw an image to the screen.

  1. Add file to data folder
  2. Create the PImage variable
  3. Load the image into the variable with loadImage()
  4. Draw the image image() function

PImage is a class for loading and displaying an image.

​

3 of 15

PRACTICE ACTIVITY

  • Example 15-1. �Use different image from the textbook.�
  • Next, control the height & width with the mouse, �i.e., Exercise 15-1

To get the image:

  • Drag file into Processing window, or
  • Click SKETCH>Add file, or
  • Create the data folder and add file manually.

Other considerations

  • Accepted files are GIF, JPG, PNG & TGA
  • Spaces are OK, but file name is case sensitive.

4 of 15

RESIZING AN IMAGE

  • Problem: �Using a 4th & 5th parameter to resize an image does not work consistently. e.g. image(img, 0, 0, width/2, height/2)
  • Solution: There is a consistency issue with how images are rendered when sized that way.
    • Ideally, should resize in image editor.
    • A workaround is to calculate by using image dimensions. e.g. �image(img, 0, 0, 240/2, 190/2)
    • The proper function is with resize()�https://processing.org/reference/PImage_resize_.html

5 of 15

ANIMATE - This is what we’ll do in Example 15-2

  • Use translate and rotate to animate an image
  • Increment or decrement going across.
  • Remember that rotate is measured in radians
  • Change mode if desired:�rectMode(CENTER)

6 of 15

ANOTHER PRACTICE EXERCISE

  • Open your Example 15-2 and let’s examine code.
  • Use your own picture, or get one from pngpix.com, www.pngimg.com, cleanpng.com, or freepngimg.com
  • For a remix, add a raster photo as background()

It’s OK to copy/paste from website. Then let’s revise…

7 of 15

SYNTAX OF TINT AND ALPHA

1 argument: tint(brightness)

2 arguments: tint(brightness, alpha)

3 arguments: tint(r, g, b)

4 arguments: tint(r, g, b, alpha)

Alpha is from 0-255, where:

0 is completely transparent

255 is completely opaque

About Alpha

Tint

8 of 15

First Example

Original

tint(255, 100);

Shows Rainbow Row behind waterfront pineapple.

Brightness and alpha

9 of 15

Another Example:

tint(255, 0, 0);

Or

tint(#FF0000);

​

Shows a red tint with no transparency

10 of 15

Another Example:

background(0);

tint(0,200,255 );

…blue/green with no transparency

background(0);

tint(0,200,255,50);

…blue/green with high transparency against black background.

11 of 15

INTERESTING COMPARISON TO “THE BOUNCING BALL”

//see page 86. Reverse polarity

PImage img;

float transparency = 255;

float speed = 1;

void setup() {

size(400, 400);

img = loadImage("flower_red.png");

}

void draw() {

background(0);

transparency = transparency - speed;

tint(255, transparency); //starts with no transparency.

image(img, 100, 80);

if ((transparency < 0) || (transparency >255) ){

speed = speed * -1;

}

println(transparency);

}

​

12 of 15

Install Video Library

  • Like the book says on pg 226, go to �Sketch>Import Library.
  • But instead of Add Library, choose Manage Library
  • Search for video

​

​

  • Choose “Video Library for Processing 4”
  • Click Install at bottom-right of screen.
  • After installed, a checkmark will show at the left of Video Library for Processing 4.
  • You may close the Contribution Manager.
  • Follow page 335 (section 16-2) to display your video

​

​

13 of 15

Click to sign up for �Show & Tell

Use the textbook or online tutorials to learn more about rotation. With your teammate, create a more embellished version of this vector scene. In addition to the aesthetics, the important programming functions are: translate, rotate, pop, and push. Specifically, you will:

  • Rotate the rays of the sun
  • Rotate the blades of the windmill
  • Rotate the petals of the flower(s)

(Try to animate the blades and sun rays when the program runs.)

  • Be sure to use functions or objects so that draw() is kept fairly clean and organized.
  • Your presentation is important. Be sure to do a good job of explaining portions of your code.

14 of 15

Click to sign up for �Show & Tell

Slideshow/Gallery

Using either Example 15-3 or 15-4 as a model, create a slideshow. Here are some specifics:

  • The primary code must come from the textbook example(s) cited above, although you can alter or add to it. It makes it easier for students to follow up with it if they desire.
  • The images must be CofC photos taken with your own camera. E.g., buildings, landscapes, iconic symbols, etc. (If you include people in your photos, be sure to get their permission.)
  • The size() should be moderate

​

15 of 15

Submit by zipping it up and uploading to a folder called �“Headroll Classwork”

​

An example is shown below.

Task: Program a sketch where your head rotates across the canvas and keeps looping around as in Example 15-2.

  • Find an image of yourself. Then use Photopea.com to crop your head into a circle.
  • Use Example 15.2 as a model, but incorporate the steps below.
    1. The size() width must be between 500-800px wide.
    2. Use the background() function to add a background image of your choice.
    3. Near the top or bottom of your design, add an image that repeats across the screen. �- Find an image with a transparent background; e.g., flower, bird, baseball, etc. �- Use a for() loop to repeat it in your desired position.