Today: variables, digital images, RGB color, for loop

PSA - What is "done" for a homework?

Themes

"Science is what we understand well enough to explain to a computer. Art is everything else we do." - Donald Knuth


Variables - 2 Things To Know

More details later here: Python Variables section in the guide.

A Python variable has a name and stores a value. We'll start with two rules of variables.

1. Creating a Variable

A variable is created in the code by a single equal sign =, like this which creates a variable named x:

x = 42

As a drawing, think of the variable as a little box, labeled with the variable's name, containing a pointer to the value stored.

alt: variable x points to value 42

2. Reading A Variable

After a variable is created, its name can appear in the code, e.g. x, and that use will retrieve whatever value was stored earlier.

Here's a little example. Suppose we set a variable named color to hold the value 'red'. Then subsequent lines can use color (no quotes or anything around it), and that retrieves the stored color.

alt: variable color points to 'red'

color = 'red'
# instead of bit.paint('red')...
bit.paint(color)
bit.move()
bit.paint(color)

This paints 2 squares 'red', or whatever value was assigned to color on the first line.

Interpreter / Hack Mode

Try the >>> Hack Interpreter. You type a little expression at the ">>>" prompt and hit the enter key. Python evaluates it, prints the resulting value on the next line. We'll use this more as we get into more Python features.

>>> 1 + 1
2
>>> 1 + 2 * 3
7

In that second example, see that Python follows the order of operations in an expression - evaluate multiplication and division before addition and subtraction.

Interpreter - Variable Demo

Suppose we want to compute the number of hours in a week. Then try defining a days variable using =...

>>> 7 * 24
168
>>> days = 7
>>> days * 24
168

Shows how a variable is set with =, and then using that variable name later retrieves the stored value.


Images - Numbers - Code

Digital Images - Pixels

See pebbles-zoomed.png

RGB Color Scheme

Image Code - Pixels, Coordinates, RGB

# Load an image from the filesystem
# into RAM in variable named "image".
# Now the image can be manipulated by code.

image = SimpleImage('flowers.jpg')

alt: image variable pointing to image made of pixels in x,y grid

Have Image, How To Change?


alt: picture of poppy

Say we have loaded an image variable as shown above. Now we want to write code to change the image in some way.

For example, let's say we want to set the blue and green values in each pixel of the image to 0. This will leave just the red values. This is called the "red channel" of the image - an image made of just its red lights.

Preamble: pixel.red

If we have a variable referring to just one pixel, then the syntax pixel.red or pixel.blue or pixel.green will refer to the red or blue or green value in that pixel.

So this code will set the red value of a pixel to 0, using the = similarly to above.

pixel.red = 0

Solution - for loop

Here is the code that solves it using a "for loop"

def red_channel(filename):
    image = SimpleImage(filename)
    for pixel in image:
        pixel.green = 0
        pixel.blue = 0
    return image

Here is a link - you can try running it first, then we'll study how it works

> Image1 Examples

How It Works - Big Picture

For Loop Operation

For loop parts:

for x in collection:
    # use x in here

Image Foreach - a. red_channel()

alt: pixel var points to each pixel in turn for each loop iteration


Side trip about math

Update Variable: x = x + 1

What does this do:

x = x + 1
x = 6
x = x + 1
# what is x now?

image1-b. Make Image Darker

    for pixel in image:
        pixel.red = pixel.red / 2
        pixel.green = pixel.green / 2
        pixel.blue = pixel.blue / 2
        # or shorthand form:
        # pixel.red /= 2

Relative Variable Shorthand: += -= *=

Shorthand way to write x = x + 1

x += 1

Shorthand for x = x * 2

x *= 2  # double x
>>> x = 10
>>> x += 3
>>> x
13
>>> x *= 2
>>> x
26

Image2 Puzzles

> Image2 Puzzles