Lab 4: Image Manipulation
This lab teaches about pixels—what they are, how they make up an image on the screen, and how manipulating their red, green, and blue values can change an image. Students begin by watching a video demonstrating that a screen is indeed made up of tiny squares of red, green, and blue. They then view an image made up of large pixels at low resolution and increase the resolution (while shrinking the pixels) to see the pixels disappear as the image becomes more visible. Students then learn about lists and tables as they explore the RGB values of pixels in an image, explore the higher-order function map
as they select a single color value from each pixel in an image, and explore several filters that apply a transformation to each pixel of an image.
Pacing
This lab is designed for 2–4 class periods (65–160 minutes).
↑ Back to Top
Activity 1: Picture of a Pixel.
↑ Back to Top
Activity 2: Exploring RGB Colors.
- Materials: Exploring RGB Colors student page
-
Prepare:
-
Learning Goals:
- Learn that different colors can be created by adjusting RGB values.
-
Activity Plan (10–20 minutes):
- Students explore RGB colors by using several Snap! blocks:
, which take three integers from 0 to 255 as input and reports a list of the those three color values.
, which takes a list of RGB values (such as the output of RGB pixel
) as input and draws a large model of a pixel with those values.
-
paint color from pixel
, which takes two inputs, a list of RGB color values (such as the output of RGB pixel
) and a resolution (selected from a dropdown menu), and paints a grid of pixels with those color values at the specified resolution. At higher resolutions, the pixels will blend into a single RGB color.
-
Standards:
- 2-DA-07: Represent data using multiple encoding schemes.
-
Solutions
This activity is exploratory and doesn't require a student submission. All the expressions that students build are displayed on the activity page.
Example color values below:
- purple example: 127, 0, 255
- black: 0, 0, 0
- turquoise (blue-green) example: 0, 175, 175
- yellow example: 255, 255, 0
- orange example: 255, 175, 0
- gray example: 127, 127, 127
- brown example: 130, 90, 0
↑ Back to Top
Activity 3: Investigating and Storing Images.
-
Materials:
We created
Google slides for this activity, but they are not up to date with the current version of the lab.
-
Prepare:
- You may wish to teach this activity through the slide deck. Review them and decide. (Please note that the activity pages have been developed further since the slides were created.)
-
Learning Goals:
- Explore various Snap! costumes and their pixels.
- Learn that images are stored as tables (lists of lists), in which each row is one pixel.
- Use
item of
to select a single pixel from a list.
- Understand a chain of functional results by discussing how the output of one function serves as the input to another function.
-
Activity Plan:
- Students learn about Snap! costumes and zoom in on an image of their choosing to see the RGB values of each pixel. (5–10 minutes)
- Optionally, students may add their own image to the Snap! project. (5–10 minutes)
- Students see how the pixels in images are stored as rows in a table, select a specific pixel from an image, and discuss how the output of one function becomes the input to another function. (10–20 minutes)
-
Tips:
- The "If There is Time" activity invites students to add their own images to the Snap! project either by taking a picture with a webcam or dragging an image file into the "Costumes" tab in Snap!. If your class (or any given student or group of students) has extra time, this can be a fun way to personalize the project and learn new Snap! features, but as with any "If There Is Time" extension, it's not necessary for understanding the learning goals of this page.
- Troubleshooting: If students exploring the "If There Is Time..." activities aren't able to use their camera with Snap!, they may need to update their browser.
-
Standards:
- 2-DA-07: Represent data using multiple encoding schemes.
-
Solutions
This activity is primarily exploratory and doesn't require a student submission. If you require that your students do the If There Is Time exercises of adding their own costumes to the project, you may opt to collect projects.
-
What does the
item of
block do?
- The
item of
block takes a position number and a list as input, and it reports the item from the list that is at the specified position number.
-
What is the output of
pixels of my costume
that becomes the input to item of
?
- The output of
pixels of my costume
is a table of pixels (a list of lists of RGB color values).
-
Describe why the expression
item of (pixels of my costume)
reports what it does.
- The
item of
block takes a position number and the pixels of my costume
table as input, and it reports the row from the table at the specified position number (that is, the RGB color values for the specified pixel).
↑ Back to Top
Activity 4: Pixel Map
per.
- Materials: Pixel
Map
per student page
-
Prepare:
- If you aren't familiar and comfortable with the higher-order function
map
, you may want to work through the activity on your own before supporting students through this activity. There is additional information about the map
function in the section on "Higher-Order List Operations and Rings" in the Snap! Reference Manual.
-
Learning Goals:
- Learn how to select a item out of a list and a single cell out of a table.
- Use
map
to apply functions to every element in a list.
- Read more complex expressions with nesting functions.
- Explore several image filters that transform each pixel in an image.
-
Activity Plan:
- Student use functions to select a specific color value from a pixel. (5–10 minutes)
- Students learn about the
map
higher-order function and use it to select only the red pixels from an image. (5–10 minutes)
- Students explore several mystery filters that manipulate an image pixel by pixel (10–30 minutes)
-
Tips:
- There are several sets of optional, extension activities on this page:
- The first "If There is Time" invites students to take a look at the code behind the
color from pixel
block. This code uses predicates and reporters, which aren't introduced until Unit 2. It's not recommended to teach this content now, but, being a fairly readable block of code, is offered as a preview of what is to come.
- The second set of "If There is Time" prompt invites students to adjust the inputs to Mystery Filter #1 to achieve other goals.
- The third "If There is Time" set asks a deeper question about multiplying zero by a percentage.
- There is also a "Take It Further" prompt that invites students to create additional image filters of their own design.
-
Standards:
- 2-DA-07: Represent data using multiple encoding schemes.
-
Solutions
-
Now write a function that use item of to select just the green color value from the 85th pixel in the pixels of my costume table.
-
How would you change your code to report just the blue color value?
-
Compare the output of the
map
expression to the output of pixels of costume
. (and)
The map
expression reports a list of only the red pixels from the pixels of costume
table.
-
What does this
map
expression do?
The map
expression reports a list of only the selected color channel from the pixels of costume
table.
-
Mystery Filter #1:
-
If an area of the resulting image is black, what does that mean about the original color?
The original color in that area had no red.
-
What kinds of images would turn mostly or completely black with this filter?
Images that have very little red in them will turn mostly black.
-
(If There Is Time) How could you make a filter that showed only the green or only the blue values?
Change the first input to the color from pixel
block to green or blue, move the color from pixel
block to the green or blue input slot of RGB pixel
, and put a 0 in its place:
-
(If There Is Time) How could you make a filter that blocked only the red, green, or blue values?
Use the color from pixel
block with the appropriate color value selected as the input for all slots in RGB pixel
except the color that you wish to block, where you should put a 0.
-
Then drag the
RGB pixel
block out of the paint color
block and into the scale pixel
block, and click the new scale pixel
expression. How does the scale pixel
block change the values of the input pixel?

The scale pixel
block scales the values of the input pixel by the input percentages.
-
What do you think will happen if you drag the
scale pixel
expression into the paint color
block and click it?

It paints an orange color on the stage.
-
Mystery Filter #2:
-
Can you make the image even brighter?
Yes, by significantly raising the percentage inputs (such as to 400%) to the scale pixel
block.
-
Can you make the image much darker?
Yes, by significantly reducing the percentage inputs (such as to 50%) to the scale pixel
block.
-
What happens if you scale different colors to different percentages of their original values?
Color channels with percentage values greater than 100% will be made brighter. Color channels with percentage values less than 100% will be made darker.
-
The
scale up
block won't change black pixels. Why not?
The RGB value of a black pixel is 0, 0, 0. Multiplying 0 by any percentage will not chance its value.
Correlation with CSTA Standards
- 2-DA-07: Represent data using multiple encoding schemes. Data representations occur at multiple levels of abstraction, from the physical storage of bits to the arrangement of information into organized formats (e.g., tables). Students should represent the same data in multiple ways. For example, students could represent the same color using binary, RGB values, hex codes (low-level representations), as well as forms understandable by people, including words, symbols, and digital displays of the color (high-level representations).