CS 111 w20 lecture 15 outline

1 Polls

s = "abc"
s = s[:1] + "MOOO" + s[1:]
print(s)
data = [[3, 1, 6],
        [2, 8, 9],
        [5, 5, 5]]
print(data[:2][-1][::-1][0])

2 2D Data

  • index multiple times to get to individual elements in a nested list
  • row-major order
  • don't really behave like a grid with rows and columns (see trying to slice a column)
    • a module called numpy provides a very useful variation on a list called an array

3 Image data

  • how to represent color?
    • printing on paper: CMYK (cyan, magenta, yellow, black)

Sorry, your browser does not support SVG.

  • digital: RGB (red, green, blue)
    • on screens, tightly packed red, green, and blue elements light with different intensities

rgb-pixels.jpg

  • many different geometries are possible

pixel-geometry.jpg

  • Samsung Galaxy Note 4

samsung-galaxy-note4-pixels.jpg

  • Samsung Galaxy Nexus

samsung-galaxy-nexus-pixels.jpg

  • Apple iPhone 5

iphone-pixels.jpg

3.1 Represent an Image in Python (2x2 example)

rgb-pixels-2x2.png

3D list!

[[[1, 0, 0], [0, 1, 0]],
 [[0, 0, 1], [1, 0, 0]]]

4 Working With Images

import matplotlib.pyplot as plt
image = plt.imread("beach_portrait.png")
  • image data as an array
    • what you see when you print
    • 0 to 1 in RGB
    • shape, size
    • len, loops the same
    • differences: fixed size, only one kind of data
  • array indexing
    • row, column
    • slicing along multiple dimensions
  • broadcast assignment

4.1 Fruit Ninja Redux

  • crop the image by 10 pixels on each side
  • slice out 10-by-10 box in bottom right corner
  • black out every other column