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
Announcements
2
Questions?
CSE 163: Numpy and Images
Numpy Arrays
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
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
Broadcasting
5
m: [[1, 1, 1],
[1, 1, 1]]
v: [0, 1, 2]
m.shape = (2, 3)
v.shape = (3,)
+
CSE 163: Numpy and Images
Rules of Broadcasting
6
CSE 163: Numpy and Images
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
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
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
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
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
(Grey-scale) Images as Matrices
12
Grey-scale: 255
Grey-scale: 0
data = iio.imread(‘...’)�
data[rows, columns] = #
CSE 163: Numpy and Images
Color Images
13
data[rows, columns, (RGB)] = #
CSE 163: Numpy and Images