1 of 13

Lecture 17: Numpy and Images

Autumn 2026

1

Sli.do

#cse163

Questions?

Icebreaker

What can you do all day and not get tired of it?

CSE 163

CSE 163: Numpy and Images

​

​

2 of 13

Announcements

  • Programming Practice 5 due Sunday, November 8 at 11:59pm on Gradescope
  • Homework 4 due Thursday, November 12 at 11:59pm on Gradescope
  • Project Part 2 (EDA) due Sunday, November 15 at 11:59pm on Gradescope

2

Questions?

CSE 163: Numpy and Images

​

​

3 of 13

Numpy Arrays

  • The primary data structure that we work with is called a numpy.array

3

a = np.array([12, 6]) # [12, 6]

b = np.arange(5) # [0, 1, 2, 3, 4]

c = np.ones(3) # [1, 1, 1]

CSE 163: Numpy and Images

​

​

4 of 13

Reshaping

Numpy arrays have a shape

(e.g. dimensions)

Reshape function allows us to shape the array without changing the data inside

4

x = np.ones((2, 3))

x: array([[1, 1, 1],

[1, 1, 1]])

y = np.arange(9)

y = y.reshape((3, 3))

​

y: array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])

CSE 163: Numpy and Images

​

​

5 of 13

Broadcasting

  • Numpy allows us to perform element-wise operations on arrays that have the same shape!
    • Some cases where these operations are possible with arrays of different shapes

5

m: [[1, 1, 1],

[1, 1, 1]]

v: [0, 1, 2]

​

m.shape = (2, 3)

v.shape = (3,)

+

CSE 163: Numpy and Images

​

​

6 of 13

Rules of Broadcasting

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.

​

  • If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.

​

  • If the sizes of any dimension disagree and neither is equal to 1 → error!

6

CSE 163: Numpy and Images

​

​

7 of 13

Broadcasting Walkthrough

7

m: [[1, 1, 1],

[1, 1, 1]]

v: [0, 1, 2]

​

m.shape = (2, 3)

v.shape = (3,)

+

CSE 163: Numpy and Images

​

​

8 of 13

Broadcasting Walkthrough

8

m: [[1, 1, 1],

[1, 1, 1]]

v: [0, 1, 2]

​

m.shape = (2, 3)

v.shape = (1, 3)

+

CSE 163: Numpy and Images

​

​

9 of 13

Broadcasting Walkthrough

9

m: [[1, 1, 1],

[1, 1, 1]]

v: [[0, 1, 2]]

​

m.shape = (2, 3)

v.shape = (1, 3)

+

CSE 163: Numpy and Images

​

​

10 of 13

Broadcasting Walkthrough

10

m: [[1, 1, 1],

[1, 1, 1]]

v: [[0, 1, 2],

[0, 1, 2]]

​

m.shape = (2, 3)

v.shape = (2, 3)

+

CSE 163: Numpy and Images

​

​

11 of 13

Practice: Broadcasting

What is the result of running this code?

11

x = np.arange(3).reshape((3, 1))

y = np.arange(3)

x + y

​

CSE 163: Numpy and Images

​

​

12 of 13

(Grey-scale) Images as Matrices

12

Grey-scale: 255

Grey-scale: 0

data = iio.imread(‘...’)�

data[rows, columns] = #

CSE 163: Numpy and Images

​

​

13 of 13

Color Images

  • When you overlap each color channel, it creates a picture!
    • Pixels on your monitor let out specific RBG light

13

data[rows, columns, (RGB)] = #

CSE 163: Numpy and Images

​

​