Pixel Mapper

Consider adding an acitivity where they try to build a requested type of filter and/or a filter of their own design (if we don't have one already). --MF, 1/10/23

Other XML tasks for Mary: remove all opacity stuff. --MF, 8/25/21

In the future, we could have a histogram/pictograph showing how much red, green, and blue is in the image, building on 1.3. Thoughts? --MF, 8/26/21

Also/alternatively, we could do the frequency analysis stuff from the Jens's Snap! 5 Data Science video. --MF, 8/29/21

This page is super long!! Cut it up into multiple pages. --MF, 1/11/23

In this activity, you'll access data about the pixels, apply a function to every pixel in an image and use that same process to filter a image by manipulating each of its pixels.

Selecting a Single Color

You can use code to access information about one color of a pixel.

  1. If your project isn't open already, open your "Image Manipulation" project.
  2. You've seen how to select one pixel from a table using item of...
    'pixels of my costume' reporting a table with 3 columns and 5 visible rows. (There is a row count of 15,552 in the upper left corner; not all rows are visible). The first column includes values ranging from 111-118; the second column includes values ranging from 111-117; the third column includes values ranging from 112-122. item (1) of (pixels of my costume) reporting {220, 220, 228}

    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.

    Click for a hint.

    Imagine you had a list of RGB color values, such as list (111) (111) (112). Consider: How could you use item of to select the green value? Then, recall that item (85) of (pixels of my costume) reports a list of RGB color values for the 85th pixel of the current costume.

  3. Talk with Your Partner How would you change your code to report just the blue color value?

The color () from pixel 'list input slot' block makes this job easier. Instead of having two item of blocks (which can be confusing), this block takes a color name and a pixel as input and it applies item of (with the correct input number) to the input pixel.

: Abstraction

Hiding the details of how color from pixel selects the correct RGB value from a pixel (so you don't have to see the extra item of block) is a form of abstraction.

  1. Click the following expression, then change the input color to "blue", and click it again. The outputs should be the same as you found before for the 85th pixel.
    color 'green' from pixel (item (85) of (pixels of my costume)
  1. The color from pixel block uses the item of block to select the right item. To see how it works, right-click color from pixel, and select "edit…". 
I want to use the draw pixel block as a one-pixel segue into mapping channel edits over an image. --MF, 8/27/21

Mapping over a List

You can use map 'reporter input slot' over 'list input slot' to apply any block that works on one list item to every item in a list.

  1. Click the following expression, which uses map to apply color from pixel to every pixel in the pixels of costume list.
    map (color ('red') from pixel 'list input slot') over (pixels of my costume))
  2. Compare the output of the map expression to the output of pixels of costume.
  3. Change the input color in the color from pixel block, and compare to pixels of costume again.
  4. Talk with Your PartnerWhat does this map expression do?
    Click for a description of what the map expression does.

    Since color (red) from pixel 'list input slot' reports a single color value from the input pixel (in this case, the red value), the expression map (color ('red') from pixel 'list input slot') over (pixels of my costume)) reports a single color value from each pixel in the pixels of costume list (in this case, the red value of each pixel).

    Click for an example of what the map expression reports.

    For example, with "red" as the input to color from pixel, the map expression reports a list of only the red values of all the costume's pixels:

    Result of pixels of costume
    Result of mapping color (red) from pixel over pixels of costume
    table with 4 columns and 8 rows showing, though there is a row count of 13,824 in the upper left corner (not all rows are visible). The first column includes values ranging from 142-145; the second column includes values ranging from 165-168; the third column includes values ranging from 197-200.
    list with 8 items showing, though there is an item count of 13,824 in the upper left corner (not all items are visible). The values of the list items range from 142-145.

: Higher-Order Function

Map is a higher-order function. That just means that it's a function that takes a function (in this case, color from pixel) as input.

This map expression
map (color ('red') from pixel 'list input slot') over (pixels of my costume))
takes two inputs:

  1. The function color from pixel: color (red) from pixel 'list input slot'
  2. The list that is the output of the function pixels of my costume (Click for a reminder of the output.)
    'pixels of my costume' reporting a table with 3 columns and 5 visible rows. (There is a row count of 15,552 in the upper left corner; not all rows are visible). The first column includes values ranging from 111-118; the second column includes values ranging from 111-117; the third column includes values ranging from 112-122.

You can tell the difference between the second input slot to map 'reporter input slot' over 'list input slot', which takes the output of a function (like most inputs do), and the first input slot, which takes a whole function itself (not its output), because the first input slot has a gray ring around it. The gray ring tells you that the map function is expecting a function (not the output of a function) as input.
gray ring

Map takes a function as input so it can apply that function to each item in the list. The empty input slot in the function (in this case, the pixel) is where each item from the list (in this case, the list of pixels) goes every time the function is applied.

Maybe give them a FYTD with a few map examples to predict and then try out in Snap! before going on... --MF, 10/15/21

Consider a page break here. --MF, 9/13/21

Playing with Filters

I dropped what had been the second filter here because I found it both too confusing and redundant with the first; I propose we leave that out. --MF, 9/13/21
  1. Mystery Filter #1:
    1. Talk with Your Partner What will the following map expression report?
      map (RGB pixel, red: (color (red) from pixel 'empty list input slot') green: (0) blue: (0)) over (pixels of costume)
    2. After making your prediction, click the expression to see what is reported. If it isn't what you expected, talk through why this is the result.
    3. Run the "Mystery Filter #1." If it's not using your favorite costume, you can change the costume in the use costume dropdown menu.
      use costume (parrots) with pixel filter ((map (RGB pixel, red: (color (red) from pixel 'empty list input slot') green: (0) blue: (0)) over (pixels of costume)))
    4. What does Mystery Filter #1 do?
    5. Image needs to be updated. --MF, 9/13/21

      Click for a description of what Mystery Filter #1 does.

      Mystery Filter #1 creates a new costume which contains only the red pixels from the original costume by setting the green and blue to zero. Mouse over the result to verify that there are no pixels with any green or blue in them.
      image of Snap! stage with and all-red photo of a person making a silly face. In the upper left corner there is an 8 by 8 grid of squares of various shades of red representing a zoomed in portion of the red photo. Below the grid are two lines of text: 'RGB:164,0,0,255' and 'XY: 95,35'

    6. Talk with Your Partner
      1. If an area of the resulting image is black, what does that mean about the original color?
      2. What kinds of images would turn mostly or completely black with this filter?
  1. How could you make a filter that showed only the green or only the blue values?
  2. How could you make a filter that blocked only the red, green, or blue values?
  1. Explore the scale red to (200) % green to (200) % blue to (200) % for pixel 'list input slot' block.
    1. Click the following instruction to paint a brown color on the stage.
      paint color, RGB: (RGB pixel, red: (128) green: (64) blue: (0))
    2. 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.Talk with Your PartnerHow does the scale pixel block change the values of the input pixel?
    3. Predict: What do you think will happen if you drag the scale pixel expression into the paint color block and click it? Try it.
  2. Mystery Filter #2:
    1. Talk with Your Partner What do you think the following map expression will to the pixels from the costume?
      map (scale red to (200) % green to (200) % blue to (200) % for pixel 'list input slot') over (pixels of costume)
    2. After making your prediction, click the expression. If the output isn't what you expected, discuss the result.
    3. Run the "Mystery Filter #2." (You can change the costume if you like.)
      use costume (parrots) with pixel filter ((map (scale red to (200) % green to (200) % blue to (200) % for pixel 'list input slot') over (pixels of costume)))
    4. What does Mystery Filter #2 do?
    5. Click for a description of what Mystery Filter #2 does.

      Mystery Filter #2 brightens the whole image by increasing the red, green, and blue values each to 200% (twice) their original value. This brings them closer to white: 255, 255, 255 (unless they started at 0).

    6. Talk with Your Partner
      1. Can you make the image even brighter?
      2. Can you make the image much darker?
      3. What happens if you scale different colors to different percentages of their original values?
  1. The scale up block won't change black pixels. Why not?
  1. Try building an image filter of your own.
Cut for now. Maybe build into an extension activity... --MF, 9/13/21
  1. Click the script under the "Filter #4: Posterize" comment, changing the costume first as desired.
  2. Filter #4 (posterization) simplifies the colors in an image. If a pixel's red is greater than the red cutoff, then it's rounded up to 255, otherwise it's rounded down to 0. The same happens for the green and blue cutoffs. Talk with Your Partner
    • What happens when you change the red cutoff? Does it become more or less red?
    • What about if you set a cutoff all the way up to 255? Or all the way down to 0?
In this activity, you learned how to select a single pixel out of an image or and a single color value out of a pixel, how to apply a function to every element of a list using map, and used map to explore several filters that transformed each pixel in the image.