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 anarray
- a module called
3 Image data
- how to represent color?
- printing on paper: CMYK (cyan, magenta, yellow, black)
- digital: RGB (red, green, blue)
- on screens, tightly packed red, green, and blue elements light with different intensities
- many different geometries are possible
- Samsung Galaxy Note 4
- Samsung Galaxy Nexus
- Apple iPhone 5
- in image files, divided up into grid, each cell is called a pixel, each pixel has a mix of red, green, and blue
- Color picker: https://www.google.com/search?q=color+picker
3.1 Represent an Image in Python (2x2 example)
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