← Workshops Free sample · Dom poster
    Python loads on first run
    Make it first · 2 min

    Print the Berliner Dom as a poster

    Your task
    1. Run the code (Ctrl+Enter).
    2. Change DARK on line 4 to 60 and run.
    3. Change it to 120 and run.

    Which poster has the most blue?

    The Berliner Dom and the TV tower as a three-colour poster: dark stone in blue, lighter stone in pink, the sky mostly cream paper with a pink band.
    What you'll make Each pixel becomes one of three flat colours, chosen by how dark it is. It looks like a screen print.

    In this lesson you will:

    • compare a pixel’s brightness with a number
    • write the rule that decides which pixels get ink
    • fix a rule where the blue never appears
    Hint
    DARK is a brightness from 0 (black) to 255 (white). Pixels darker than it get blue.
    Hint
    A higher DARK makes more of the photo blue. Keep it below MID (150), or the pink disappears.
    Solution
    Put line 4 back to DARK = 90 and run. That is the starting code, and it runs.
    poster_dom.py Ctrl+Enter runs
    Run your code to check it.
    Output

    What your code prints and draws appears here.

    Concept 1 of 3 · 1 min

    A photo is a grid of numbers

    A digital photo is a grid of tiny squares called pixels: here 300 rows by 480 columns, 144,000 pixels. Each pixel stores three numbers (red, green, blue), each from 0 (none) to 255 (full). To print in three colours we need one number that says how dark a pixel is. We use the average of the three: the brightness, from 0 (black) to 255 (white). The code calls it light.

    Worked example

    The marked pixel is (90, 88, 83): (90 + 88 + 83) / 3 = 87, a dark grey.

    Check yourself

    A pixel is (90, 30, 0). What is its brightness?

    Show the answer

    40: (90 + 30 + 0) / 3 = 40.

    The Berliner Dom photo with a small square marked on the dome's lantern. Beside it the 6 by 6 pixels of that square, enlarged, each in its real colour with its brightness number, from 58 to 218. One is marked: (90 + 88 + 83) / 3 = 87. The Berliner Dom photo with a small square marked on the dome's lantern. Beside it the 6 by 6 pixels of that square, enlarged, each in its real colour with its brightness number, from 58 to 218. One is marked: (90 + 88 + 83) / 3 = 87.
    A 6 × 6 patch of the Dom, enlarged. Each square is one pixel; the number is its brightness.
    Step 1 · Predict · 3 min

    Ask a yes/no question

    Your task
    1. Guess first: which numbers will print True?
    2. Run.
    3. Change THRESHOLD on line 1 so that exactly three of the four squares get ink.
    Notes for step 1
    Concept
    The Berliner Dom photo with a small square marked on the dome's lantern. Beside it the 6 by 6 pixels of that square, enlarged, each in its real colour with its brightness number, from 58 to 218. One is marked: (90 + 88 + 83) / 3 = 87. The Berliner Dom photo with a small square marked on the dome's lantern. Beside it the 6 by 6 pixels of that square, enlarged, each in its real colour with its brightness number, from 58 to 218. One is marked: (90 + 88 + 83) / 3 = 87.
    A photo is a grid of numbers

    Each pixel holds a red, a green and a blue number from 0 to 255; their average is its brightness.

    Open the concept: A photo is a grid of numbers
    Watch out

    Watch out

    line 4

    120 < 120 is False: a pixel exactly at the threshold stays paper.

    In the code:

    • light is a pixel’s brightness.
    • THRESHOLD is a number you choose.

    light < THRESHOLD asks one question: is this pixel darker than that number? Python answers True or False.

    The Dom photo with four pixels circled: stone in shadow, sky, green dome, stone. Each has three numbers, red, green and blue, and one brightness: 30, 200, 120 and 90. The Dom photo with four pixels circled: stone in shadow, sky, green dome, stone. Each has three numbers, red, green and blue, and one brightness: 30, 200, 120 and 90.
    The four numbers in the code are the brightness of these four pixels.
    Hint
    30 < 100 is True; 200 < 100 is False. Do the other two the same way.
    Hint
    The three darkest numbers are 30, 90 and 120. The threshold must be above 120 and no more than 200.
    Solution
    THRESHOLD = 150
    Explain it another way

    Picture a ruler from 0 (black) to 255 (white). Your four numbers sit on it: 30, 90, 120 and 200.

    THRESHOLD is a mark on that ruler:

    • left of the mark: True, gets ink
    • on the mark or right of it: stays paper

    Where must the mark go so that three numbers are on its left?

    step1_compare.py Ctrl+Enter runs
    Run your code to check it.
    Output

    What your code prints and draws appears here.

    Concept 2 of 3 · 1 min

    One threshold, every pixel

    A threshold is one number on the brightness scale. The rule asks every pixel one yes/no question, light < THRESHOLD. True gets ink, False stays paper. The grid of True/False answers is called a mask: it shows where the ink goes. Splitting a picture in two like this is called thresholding. Raise the threshold and more pixels fall below it.

    Worked example

    The marked pixel from concept 1: 87 < 110 is True, so it gets ink. A light stone pixel: 215 < 110 is False, so it stays paper.

    Check yourself

    The threshold rises from 60 to 120. Does the poster get more blue or less?

    Show the answer

    More: More pixels fall below 120 than below 60: the inked part grows from about 19 % to 35 % of the photo.

    Three 6 by 6 grids of the same Dom pixels: their brightness numbers, then T or F for light < 110, then blue where T and paper where F. Three 6 by 6 grids of the same Dom pixels: their brightness numbers, then T or F for light < 110, then blue where T and paper where F.
    The same 36 pixels three times: their brightness, the answer to light < 110, and the result in ink or paper.
    Step 2 · Write · 2 min

    Ink the dark pixels

    Your task
    Replace ___ with a comparison, so that pixels darker than THRESHOLD get BLUE.
    Notes for step 2
    Concept
    Three 6 by 6 grids of the same Dom pixels: their brightness numbers, then T or F for light < 110, then blue where T and paper where F. Three 6 by 6 grids of the same Dom pixels: their brightness numbers, then T or F for light < 110, then blue where T and paper where F.
    One threshold, every pixel

    Every pixel is compared with the same number: darker than it gets ink, the rest stays paper.

    Open the concept: One threshold, every pixel

    The rule is a Python function: it takes one brightness and returns one colour. paint calls it for every pixel, all 144,000, and builds the poster from the results.

    Hint
    You wrote this question in step 1. Which two names does it compare?
    Hint
    Line 6 names the brightness light. Line 2 sets the number to compare it with, THRESHOLD.
    Solution
    if light < THRESHOLD:
    Explain it another way

    Follow two pixels through the rule. THRESHOLD is 110.

    1. A dark pixel: light is 87. It should get BLUE, so the if must be True.
    2. A light stone pixel: light is 215. It should get PAPER, so the if must be False.

    Which question from step 1 does that?

    step2_rule.py Ctrl+Enter runs
    Run your code to check it.
    Output

    What your code prints and draws appears here.

    Concept 3 of 3 · 1 min

    Two thresholds, three colours

    Two thresholds cut the brightness scale into three bands: below 90 blue, from 90 to 149 pink, 150 and up paper. In the rule each band is an if with a return. A return ends the rule for that pixel, so the lines below it never run. A screen print works in bands too: one stencil per colour, each printed flat.

    Worked example

    A pixel at 170: 170 < 90 is False, 170 < 150 is False, so the rule reaches its last line and returns PAPER.

    Check yourself

    The bands are at 90 and 150. Which colour does a pixel at 120 get?

    Show the answer

    Pink: 120 is not below 90, but it is below 150.

    The 0 to 255 brightness ruler with marks at 90 and 150: blue below 90, pink from 90 to 149, paper from 150. Next to it the three-colour poster of the Dom. The 0 to 255 brightness ruler with marks at 90 and 150: blue below 90, pink from 90 to 149, paper from 150. Next to it the three-colour poster of the Dom.
    Two thresholds cut the ruler into three bands of colour.
    Step 3 · Fix · 3 min

    The blue that never shows

    Your task

    This rule should give three colours: blue, pink and paper. No blue shows.

    Change the order of the two ifs so the blue appears.

    Notes for step 3
    Concept
    The 0 to 255 brightness ruler with marks at 90 and 150: blue below 90, pink from 90 to 149, paper from 150. Next to it the three-colour poster of the Dom. The 0 to 255 brightness ruler with marks at 90 and 150: blue below 90, pink from 90 to 149, paper from 150. Next to it the three-colour poster of the Dom.
    Two thresholds, three colours

    Two thresholds split the brightness scale into three bands: blue, pink and paper.

    Open the concept: Two thresholds, three colours
    Why it worked

    Why it worked

    lines 7 and 9
    Two rulers. With the below-90 question first: blue, pink and paper bands, and a pixel at 10 lands in blue. With the below-150 question first: only pink and paper; the pixel at 10 stops in pink. Two rulers. With the below-90 question first: blue, pink and paper bands, and a pixel at 10 lands in blue. With the below-150 question first: only pink and paper; the pixel at 10 stops in pink.
    The same two ifs in both orders. A pixel at 10 stops at the first True.

    In a chain of ifs the first True wins, so the narrow question (below 90, fewer pixels) goes before the wide one (below 150, more pixels).

    Three colours need two questions:

    • below 90: blue
    • from 90 to 149: pink
    • 150 and up: paper

    Python reads the ifs from the top.

    Hint
    A pixel at 10 is below 150, so the first if already returns pink for it. Which if should come first?
    Hint
    Check light < 90 first, then light < 150. Keep the numbers.
    Solution
    Move if light < 90: and its return BLUE above if light < 150:.
    Explain it another way

    Picture two doors in a row, one for each if. A pixel walks through the first door that lets it in; return means it stops there.

    1. The wide door (below 150) lets in every pixel under 150, the dark ones too.
    2. So if it comes first, no pixel ever gets through the narrow door (below 90).

    Which door must come first?

    step3_fix.py Ctrl+Enter runs
    Run your code to check it.
    Output

    What your code prints and draws appears here.

    Wrap up · 2 min

    What you can do now

    Answer from memory first: one question for each concept. Try not to look back.

    Recall 1 of 3

    What is the brightness of the pixel (200, 40, 60)?

    Show the answer

    100: (200 + 40 + 60) / 3 = 100: the weak green and blue pull the strong red down.

    Recall 2 of 3

    The rule makes a pixel BLUE when light < 100. A pixel's brightness is exactly 100. Does it turn BLUE?

    Show the answer

    No, it stays PAPER: 100 < 100 is False, so the pixel stays PAPER.

    Recall 3 of 3

    Python reads these checks from the top. Which colour does a pixel at 50 get?

    if light < 160:
        return PINK
    if light < 80:
        return BLUE
    return PAPER
    

    Show the answer

    Pink: 50 is below 160, so the first check answers and the blue check is never reached.

    • A pixel is three numbers; their average is its brightness, 0 to 255.
    • < asks a yes/no question; a function turns the answer into a colour.
    • In a chain of ifs the first True wins, so the narrow question comes first.

    Photo: Ansgar Koreng, Berliner Dom und Fernsehturm, 2017, CC0.

    Sources (3)
    1. Gonzalez, R. C., & Woods, R. E. (2018). Digital image processing (4th ed.). Pearson.
    2. Kipphan, H. (Ed.). (2001). Handbook of print media: Technologies and production methods. Springer. https://doi.org/10.1007/978-3-540-29900-4
    3. Koreng, A. (2017). Berliner Dom und Fernsehturm, 170117, ako.jpg [Photograph]. Wikimedia Commons. https://commons.wikimedia.org/wiki/File:Berliner_Dom_und_Fernsehturm,_170117,_ako.jpg (CC0 1.0)
    Go further 1 of 3 · Extend · optional

    A fourth colour

    Your task
    1. Under PINK, add YELLOW = [245, 200, 60].
    2. Add one if so that pixels from 150 to 199 get YELLOW.

    Your rule from step 3 gives three colours. The target below has four.

    The Dom poster in four colours: blue, pink, yellow and cream paper. The sky is mostly yellow; the stone is mostly blue and pink, with yellow highlights. The Dom poster in four colours: blue, pink, yellow and cream paper. The sky is mostly yellow; the stone is mostly blue and pink, with yellow highlights.
    The target: four colours.
    Hint
    Copy the shape of the if above it. The new if goes after light < 150 and before return PAPER.
    Solution
    Pixels from 150 to 199 now get YELLOW.
    my_poster.py Ctrl+Enter runs
    No check on this screen: run it and play with it.
    Output

    What your code prints and draws appears here.

    Go further 2 of 3 · Combine · optional

    Grain, like a real print

    Your task
    1. Copy the four grain lines into your code, above show.
    2. In show, change photo to grainy.
    3. Run with GRAIN at 0, 40 and 80.

    Where does the grain show most?

    GRAIN = 20                              # try 0, 40, 80
    rng = np.random.default_rng(1)
    noise = rng.integers(-GRAIN, GRAIN + 1, (300, 480, 1))
    grainy = np.clip(photo + noise, 0, 255)

    rng.integers gives every pixel its own number from -GRAIN to GRAIN. The code adds it to the pixel’s red, green and blue before your rule runs. np.clip keeps the sums between 0 and 255.

    The Dom poster in four colours with fine grain: the sky flickers between pink, yellow and cream paper, and the edges between colours are speckled. The Dom poster in four colours with fine grain: the sky flickers between pink, yellow and cream paper, and the edges between colours are speckled.
    The target at GRAIN = 20.
    Hint
    Your rule stays the same. Only the picture it gets changes: grainy instead of photo.
    Solution
    The sky is close to 150, so its pixels flip between PINK and YELLOW most.
    my_poster.py Ctrl+Enter runs
    No check on this screen: run it and play with it.
    Output

    What your code prints and draws appears here.

    Go further 3 of 3 · Generalise · optional

    Any number of colours

    Your task
    1. Under PAPER, add COLOURS = [BLUE, PINK, YELLOW, PAPER].
    2. Replace the ifs in colour_of with one line that finds band, then return COLOURS[band].
    3. Add PURPLE = [120, 60, 140] under BLUE and put it in the list after BLUE.

    len(COLOURS) is the number of colours in the list, and COLOURS[band] is the colour at that place in it, counting from 0. Your line works for any length.

    The Dom poster in five colours with fine grain, from dark blue through purple and pink to yellow and cream paper. The Dom poster in five colours with fine grain, from dark blue through purple and pink to yellow and cream paper.
    The target: five colours, GRAIN = 20.
    Hint
    With 4 colours, each band is 64 brightness numbers wide. int(...) cuts off the decimals.
    Solution
    Brightness times the number of colours, divided by 256, is a number from 0 up to just below len(COLOURS).
    my_poster.py Ctrl+Enter runs
    No check on this screen: run it and play with it.
    Output

    What your code prints and draws appears here.

    Finish

    What comes next