Programming, problem solving, and algorithms
CPSC 203, 2025 W2
January 16, 2026
Colour and Code
Make the poppies pop!
PC: https://www.epicgardening.com/spring-wildflowers-from-seed/
Examine each pixel. If its red color channel is high (over 200, say), then leave it as is. Otherwise, change the pixel to grey by setting all 3 color channels to the average of their original values.
Colour and Code
import numpy as np
from skimage import io
import matplotlib.pyplot as plt
Pixel = tuple [np.uint8, np.uint8, np.uint8] # (r, g, b)
'''Input: Pixel p
Output: Pixel
Function: If the pixel has high red value, then don't change it, otherwise
fade the pixel to grey. Note, to grey a pixel, set all color channels to
the average of the 3.
'''
def boost_red_pixel(p: Pixel) -> Pixel:
# setup -- extract the color channels as ints
red = int (p[0 ])
green = int (p[1 ])
blue = int (p[2 ])
# compute grey
grey = (red + green + blue)// 3
# conditionally change the pixel to grey
if red < 200 :
red = green = blue = grey
# return updated pixel
return (red, green, blue)
def fancify(img: np.ndarray) -> np.ndarray:
H, W = img.shape[:2 ] # grab the height and width of the image
out = img.copy() # write to a copy
# traverse every pixel in the image and change it!
for row in range (H):
for col in range (W):
out[row, col] = boost_red_pixel(img[row,col])
# return image with changed pixels
return out
def main() -> None :
img = io.imread('https://raw.githubusercontent.com/UBC-CS/cpsc203/refs/heads/main/images/poppyfield.jpg' )[..., :3 ] # keep first 3 channels (RGB)
out = fancify(img)
plt.imshow(out)
plt.axis('off' )
plt.show()
main()
Your turn: Change image or boost
Experiment with the code! Try one or more of these:
Find a different image URL and boost a different color (green? blue?)
Change the threshold value (try 150 instead of 200)
Instead of greying, try inverting: red = 255 - red
What happens if you boost pixels that are LOW in red instead of high?
A little math puzzle break
\(1 + 3 + 5 + \ldots + 1337\) = ____________
How many terms?
Always always start this conversation from k=1, not zero.
puzzle continued…
English description:
The sum of the first \(k\) odd integers is ________
What about \(11 + 13 + \ldots + 23\) ?
And \(2 + 4 + 6 + \ldots + 1338\) ?
Representing handcraft
How do we represent a craft project in code?
We need to think about:
What data describes the object?
What operations can we perform on it?
Let’s start with something simpler than a full quilt…
A bead bracelet
What data describes a bead ?
What data describes a bracelet ?
Bead: radius, color Bracelet: radius, list of beads
Dataclasses in Python
A dataclass bundles related data together into a custom type.
This creates a new type called Bead with two attributes.
Your turn: Beads
What would these print?
Adding behavior: methods
A dataclass can also have methods .
What you learned today
The sum of the first k odds is _____
Lists are more complex than they first appear.
Dataclasses let us define our own types
Attributes are the data associated with an instance of a class
Methods are functions that belong to a class
Next time: The Bracelet