1 of 22

AddisCoder: Week 2

Lecture 8B:

  • Images
  • Pixels
  • Image Manipulation

2 of 22

Images

3 of 22

Black and White Images

4 of 22

Black and White Images

5 of 22

Black and White Images

6 of 22

Black and White Images

7 of 22

Black and White Images

8 of 22

Black and White Images

9 of 22

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

10 of 22

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

11 of 22

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=

12 of 22

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=

13 of 22

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)

14 of 22

Black and White Images

Practice Question: write a function to invert a black and white image.

15 of 22

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

16 of 22

Grayscale Images

17 of 22

Grayscale Images

Each pixel has a brightness value between 0 and 255, inclusive

0

186

255

15

18 of 22

Color Images

19 of 22

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]

20 of 22

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

21 of 22

Color Images

[234, 252, 31]

[141, 195, 235]

[0, 0, 0]

[30, 90, 175]

[255, 255, 255]

22 of 22

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.