CPSC 120 Principles of Computer Science Fall 2024

Lab 12
Images

Due: Fri 12/6 at the start of class

Labs are due at the start of class. It is OK if you show up and copy your files to the handin directory at the very beginning of class, but this should take at most a couple of minutes and you should not spend the next lab period finishing up the previous week's lab.


Introduction

We've spent most of the course drawing our own pictures - but incorporating images can lead to interesting effects as well. Images can be used like painted movie backdrops, manipulated (like you might do in Photoshop) to achieve a new effect, or used as the basis for your own drawing (e.g. tiling the window with triangles whose colors are derived from the image).

In this lab you'll try out some image manipulation of your own.


Successfully completing this lab means that you are able to:

Academic Integrity and Collaboration

The short version:

Review the discussion in previous lab handouts and the full collaboration policy for more details.

Handin

To hand in your work:


Preliminaries

Getting Started

Refer to the slides and examples from class for the patterns for using images as a source of colors (for the artistic effects) and for implementing image filters.

Type Casting

You might find yourself needing an int value when you instead have a float. You can do this conversion with typecasting:

   (int)(...)

where the ... is the expression that you want to convert. For example, the following gets a random number between 0 and 100 and then converts the resulting value to an integer before storing it in x:

 int x = (int)(random(0,100));

Casting a float to an int drops the fractional part of the value so if random(0,100) returned 83.67, the cast results in x getting the value 83.

Constraining Values

The constrain function (look it up in the Processing API) can be useful for keeping values in a certain range (such as within a certain drawing area). You generally don't need to worry about constraining color values to be between 0 and 255 because the color function that builds a color will do that for you, but constrain might be useful in other cases, depending on what filters and effects you choose to implement.

Turning Off Smoothing

Some of the artistic effects refer to "turning off smoothing" — this can be necessary to avoid slight gaps between adjacent shapes. To do this, put the statement

  noSmooth();

in setup().


Exercises

Put your name and a description of the sketch in comments at the beginning of each sketch. Also don't forget to Auto Format your code before handing it in. (Refer back to lab 2 for more on Auto Format if needed.)

Getting images to use —

You are also free to use other images but you must have permission for all images that you use. This means that the image is one of the provided images above, a picture you took or created, a picture someone else took or created and gave you permission to use, or an image that you found on the Internet that explicitly allows you to use it. There are many public domain images that are fine to use, but not every image is public domain! Include a comment in your sketch identifying the source for each image that you use. (Give the URL if the image came from the Internet.)


Exercise 1

Your task is to create a sketch named lab12a which applies an artistic effect to an image. Choose one of the artistic effects described in the "Artistic Effects" section below.

Leave a border of the background showing as in the example. (The background can be any color; it doesn't have to be black.) Create a drawing function for the artistic effect.

To create this sketch:


Exercise 2

Note: image filters will be covered in class on Monday 11/25 — wait until after that class to start on this exercise.

Your task is to create a sketch named lab12b which applies a custom image filter to an image. Choose one of the image filters described in the "Image Filters" section below.

Leave a border of the background showing as in the example. (The background can be any color; it doesn't have to be black.) Create a filter function for the image filter.

To create this sketch:


Exercise 3

Create a sketch named lab12c which showcases several image filters and artistic effects. For full credit, your sketch must meet the following requirements:


Extra Credit

You can earn extra credit by implementing additional image filters and/or artistic effects. There are lots of interesting things you can do with images! Some possibilities:

More creative and challenging elements will earn more points.


Artistic Effects

An "artistic effect" consists of drawing a pattern of shapes whose color is drawn from an image. The image itself isn't actually drawn on the screen.

Create a drawing function for each artistic effect. The drawing function should have parameters listed.

Each effect is illustrated using the image shown to the right as the source image. Click on the images for a larger version.


effectexamplefunction parameters how it works
pixellate
  • source image
  • position and size of the drawing area
  • rectangle size
Cover the drawing area with rectangles, taking the fill and stroke colors of each rectangle from the image pixel corresponding to the center of the rectangle. You may need to turn off smoothing to avoid slight gaps between the rectangles. Note: be careful not to draw rectangles that extend past the edge of the image.
Your function should work with any size rectangle, but experiment with different values to find one with a pleasing effect to use in your sketch.
cross-stitch
  • source image
  • position and size of the drawing area
  • rectangle size
  • line color
This is the same as pixellate, except that the rectangles have an line around them.
Your function should work with any size rectangle and any line color, but experiment with different values to find a combination that yields a pleasing effect to use in your sketch.
triangle pixellate
  • source image
  • position and size of the drawing area
  • triangle size
Cover the drawing area with triangles, taking the fill and stroke colors of each triangle from the image pixel corresponding to the center of the triangle. You may need to turn off smoothing to avoid slight gaps between the triangles. Note: be careful not to leave gaps at the edge of the image, or to draw triangles past the edge — this takes some careful handling to get right.
Your function should work with any size triangle, but experiment with different values to find a combination that yields a pleasing effect to use in your sketch.
circle pixellate
  • source image
  • position and size of the drawing area
  • circle size
Cover the drawing area with circles, taking the fill and stroke colors of each circle from the image pixel corresponding to the center of the circle. Note: be careful not to draw circles that extend past the edge of the image.
Your function should work with any size circle, but experiment with different values to find one that yields a pleasing effect to use in your sketch. The example uses a white background under the circles. You can also try a black background if you want.
variable-sized circle pixellate
  • source image
  • position and size of the drawing area
  • maximum circle size
This is similar to the circle pixellate, except that the size of the circle is determined by the grayscale color of the pixel so that brighter colors get bigger circles. (See the description of the grayscale filter for information on how to compute the grayscale color.) Note: be careful not to draw circles that extend past the edge of the image.
Your function should work with any maximum circle size, but experiment with different values to find one that yields a pleasing effect to use in your sketch. The example uses a white background under the circles. You can also try a black background if you want.
crosses
  • source image
  • position and size of the drawing area
  • cross size
  • number of crosses
  • stroke width (optional)
  • transparency (optional)
Place a large number of crosses (a vertical line and a horizontal line) at random positions, taking the color of each cross from the image pixel corresponding to the center of the cross. Note: be careful not to draw crosses that extend past the edge of the image.
Your function should work with any cross size and any number of crosses, but experiment with different values to find a combination that yields a pleasing effect to use in your sketch. You can also include (and experiment with) stroke width and/or transparency if you want.
pencil sketch
  • source image
  • position and size of the drawing area
  • line length
  • line angle
  • number of lines
  • stroke width (optional)
  • transparency (optional)
Place a large number of lines at random positions, taking the color of each line from the image pixel corresponding to the midpoint of the line. Note: be careful not to draw lines that extend past the edge of the image.
Your function should work with any line length and angle and any number of lines, but experiment with different values to find a combination that yields a pleasing effect to use in your sketch. You can also include (and experiment with) stroke width and/or transparency if you want.

Image Filters

An image filter produces a new image whose pixels are transformed in some way from the original image.

Create a filter function for each image filter. The function should have parameters listed and should create and return a new image for the filtered image.

Each filter is illustrated using the image shown to the right as the source image. Click on the images for a larger version.


filterexamplefunction parameters how it works
blend
  • source image
  • second source image
This combines two images by superimposing one on the other. For each color component (red, green, blue), average the value from the corresponding pixel in each of the source images. Make sure you use two images that are exactly the same size for this!
contrast
  • source image
  • contrast factor
The idea behind contrast is making the brighter pixels brighter and the darker pixels darker. For each color component (red, green, blue), subtract 127, multiply by the contrast factor, and then add 127 back again.
Your function should work with any contrast factor, but experiment with different values for the contrast factor to find one with a pleasing effect to use in your sketch. (Values bigger than 1 will increase the contrast, values less than 1 will decrease it.)
gray
  • source image
This makes an image whiter, much like when buttons or menu items that aren't available are grayed out. For each color component (red, green, blue), average that value with white (255).
grayscale
  • source image
Gray means that the red, green, and blue components of a color have the same value. To take into account that the fact the human visual system is not equally sensitive to all wavelengths of light, use a weighted average: combine 30% of the red value, 59% of the green value, and 11% of the blue value to get the gray value instead of the simple average (red+green+blue)/3. Use the gray value for each of the red, green, and blue components of the destination color.
Math note: combining percentages of values means to multiply each value by the corresponding percentage, then sum the results i.e. the weighted average described is computed as 0.30*red+0.59*green+0.11*blue.
invert
  • source image
This makes the picture look like a photographic negative. For each color component (red, green, blue), subtract the value of the color from 255.
noise
  • source image
  • maximum color shift
For each color component (red, green, blue), add a small random amount between -maxcolorshift and maxcolorshift to the value.
Your function should work with any max color shift, but experiment with different values to find a something that has a pleasing (and visible) effect to use in your sketch.
posterize
  • source image
  • number of levels
This reduces the number of colors in the image (something that used to be done when printing posters, hence the name). The idea is that the full range of color values from 0-255 is reduced to just a few particular colors. For example, posterizing with 10 levels means that all colors 0-24 would be replaced by 0, colors 25-51 would be replaced by 25, colors 52-76 would be replaced by 52, etc — the range 0-255 is broken into 10 equal-sized chunks. This process should be done for each color component (red, green, blue).
Hint: to calculate which level a color value belongs to, multiply the color value by the number of levels you want to posterize into, divide by 256, and then type-cast to an integer. To find the new color value, multiply the level by by 256 and divide by the number of levels.
Your function should work with any number of levels, but experiment to find a value which leads to a nice result in your sketch.
threshold
  • source image
  • threshold value
This also reduces the number of colors, but more drastically than posterize. For each color component (red, green, blue), set it to 255 if the value is greater than the threshold value and 0 otherwise.
Your function should work with any threshold value, but experiment with different thresholds to find a nice result to use in your sketch.
sepia
  • source image
This gives the image an old-time photograph look. First compute the grayscale value for the pixel (see the grayscale filter), then add 80 to the grayscale color to get the red value, add 43 to the grayscale color to get the green value, and subtract 23 from the grayscale color to get the blue value.