AddisCoder: Week 2
Lecture 8B:
Images
Black and White Images
Black and White Images
Black and White Images
Black and White Images
Black and White Images
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
Black and White Images
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
Black and White Images
Image is a grid of pixels
Each pixel has one of two
brightness values: 0 or 255
0 is lowest brightness: black
255 is highest brightness: white
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
Black and White Images
Image is a grid of pixels
Each pixel has one of two
brightness values: 0 or 255
0 is lowest brightness: black
255 is highest brightness: white
255 | 255 | 255 | 0 | 0 | 0 | 255 | 255 | 255 | 255 |
255 | 255 | 255 | 255 | 0 | 0 | 0 | 255 | 255 | 255 |
255 | 255 | 255 | 255 | 0 | 0 | 0 | 255 | 255 | 255 |
255 | 255 | 255 | 255 | 255 | 0 | 0 | 0 | 255 | 255 |
255 | 255 | 255 | 255 | 255 | 0 | 0 | 0 | 255 | 0 |
255 | 255 | 255 | 255 | 255 | 255 | 0 | 0 | 0 | 0 |
255 | 255 | 255 | 255 | 255 | 255 | 0 | 0 | 0 | 0 |
255 | 255 | 255 | 255 | 0 | 0 | 0 | 0 | 0 | 0 |
255 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Black and White Images
Image is a grid of pixels
Each pixel has one of two
brightness values: 0 or 255
0 is lowest brightness: black
255 is highest brightness: white
[[255, | 255, | 255, | 0, | 0, | 0, | 255, | 255, | 255, | 255], |
[255, | 255, | 255, | 255, | 0, | 0, | 0, | 255, | 255, | 255], |
[255, | 255, | 255, | 255, | 0, | 0, | 0, | 255, | 255, | 255], |
[255, | 255, | 255, | 255, | 255, | 0, | 0, | 0, | 255, | 255], |
[255, | 255, | 255, | 255, | 255, | 0, | 0, | 0, | 255, | 0], |
[255, | 255, | 255, | 255, | 255, | 255, | 0, | 0, | 0, | 0], |
[255, | 255, | 255, | 255, | 255, | 255, | 0, | 0, | 0, | 0], |
[255, | 255, | 255, | 255, | 0, | 0, | 0, | 0, | 0, | 0], |
[255, | 0, | 0, | 0, | 0, | 0, | 0, | 0, | 0, | 0], |
[0, | 0, | 0, | 0, | 0, | 0, | 0, | 0, | 0, | 0]] |
image=
Black and White Images
Image is a grid of pixels
Each pixel has one of two
brightness values: 0 or 255
0 is lowest brightness: black
255 is highest brightness: white
[[255, | 255, | 255, | 0, | 0, | 0, | 255, | 255, | 255, | 255], |
[255, | 255, | 255, | 255, | 0, | 0, | 0, | 255, | 255, | 255], |
[255, | 255, | 255, | 255, | 0, | 0, | 0, | 255, | 255, | 255], |
[255, | 255, | 255, | 255, | 255, | 0, | 0, | 0, | 255, | 255], |
[255, | 255, | 255, | 255, | 255, | 0, | 0, | 0, | 255, | 0], |
[255, | 255, | 255, | 255, | 255, | 255, | 0, | 0, | 0, | 0], |
[255, | 255, | 255, | 255, | 255, | 255, | 0, | 0, | 0, | 0], |
[255, | 255, | 255, | 255, | 0, | 0, | 0, | 0, | 0, | 0], |
[255, | 0, | 0, | 0, | 0, | 0, | 0, | 0, | 0, | 0], |
[0, | 0, | 0, | 0, | 0, | 0, | 0, | 0, | 0, | 0]] |
image=
Black and White Images
Image is a grid of pixels
Each pixel has one of two
brightness values: 0 or 255
0 is lowest brightness: black
255 is highest brightness: white
[[255, | 255, | 255, | 0, | 0, | 0, | 255, | 255, | 255, | 255], |
[255, | 255, | 255, | 255, | 0, | 0, | 0, | 255, | 255, | 255], |
[255, | 255, | 255, | 255, | 0, | 0, | 0, | 255, | 255, | 255], |
[255, | 255, | 255, | 255, | 255, | 0, | 0, | 0, | 255, | 255], |
[255, | 255, | 255, | 255, | 255, | 0, | 0, | 0, | 255, | 0], |
[255, | 255, | 255, | 255, | 255, | 255, | 0, | 0, | 0, | 0], |
[255, | 255, | 255, | 255, | 255, | 255, | 0, | 0, | 0, | 0], |
[255, | 255, | 255, | 255, | 0, | 0, | 0, | 0, | 0, | 0], |
[255, | 0, | 0, | 0, | 0, | 0, | 0, | 0, | 0, | 0], |
[0, | 0, | 0, | 0, | 0, | 0, | 0, | 0, | 0, | 0]] |
image=
Pixel (0,0)
Pixel (9,0)
Pixel (0,9)
Pixel (9,9)
Black and White Images
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
Practice Question: write a function to invert a black and white image.
Black and White Images
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
def make_negative(pixels): for row in range(len(pixels)): for col in range(len(pixels[row])): if pixels[col][row] == 0: pixels[col][row] = 255 else: pixels[col][row] = 0 return pixels |
Grayscale Images
Grayscale Images
Each pixel has a brightness value between 0 and 255, inclusive
0
186
255
15
Color Images
Color Images
A color pixel has three brightness values:
R(ed), G(reen), and B(lue)
��
[255, 0, 0]
[0, 255, 0]
[0, 0, 255]
[255, 255, 0]
[255, 0, 255]
[0, 255, 255]
Color Images
Question: But didn’t we learn that the 3 primary colors were Red, Blue, and Yellow? Then why do we use Red, Blue, and Green as the colors of the pixels.
Answer: Too long to cover in lecture. Read this post: www.shortl.io/why-no-yellow
Color Images
[234, 252, 31]
[141, 195, 235]
[0, 0, 0]
[30, 90, 175]
[255, 255, 255]
Color Images
A color image is a 3D list: rows, columns, and RGB
image = [
[[234, 252, 31], [0, 0, 0], [0, 0, 0]],
[[234, 252, 31], [30, 90, 175], [234, 252, 31]],,
[[30, 90, 175], [141, 195, 235], [0, 0, 0]],
[[141, 195, 235], [141, 195, 235], [255, 255, 255]]
]
So now, each pixel in each row is itself a list of 3 colors that make up that pixel.