weeks3-4 jython

Upload: seggy7

Post on 02-Jun-2018

244 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Weeks3-4 Jython

    1/98

    1

    Introduction to Programming

    Week 3: Slides 1-68

    Week 4: Slides 69-98

  • 8/10/2019 Weeks3-4 Jython

    2/98

  • 8/10/2019 Weeks3-4 Jython

    3/98

    3

    School of Computer Science & IT

    We perceive light different from how it actually is

    Colour as we know it theoreticallyis continuous It is described by physics in terms of various formulae

    It isnt discrete in the sense of having a whole number=colour Visiblelight is in the wavelengths between 370 and 730 nanometers

    Thats 0.00000037 and 0.00000073 meters

    this means that the repeating pattern of a sine curve whichrepresents light is more than tiny.

    Our eyesthough are the devices through which we see light/colour. Theyare limited in their accuracy/sensitivity

    Weperceive light with colour sensors that peak around 425nm (blue), 550 nm (green), and 560 nm (red).

    Our brain figures out the colour by figuring out how much ofeach kind of sensor is responding

    One implication: We perceive two kinds of orange onethats spectraland one thats red+yellow (hits our coloursensors just right)

    Dogs and other simpler animals have only two kinds of sensors They dosee colour. Just lesscolour, or less of a range of colours.

  • 8/10/2019 Weeks3-4 Jython

    4/98

    4

    School of Computer Science & IT

    Luminance vs. Colour

    We perceive bordersof things, motion,

    depth via luminance Luminance is notthe amount oflight, but ourperceptionof theamount of light.

    We see blue as darker than red,even if same amount of light.

    Much of our luminanceperception is based oncomparison to

    backgrounds, not raw

    Luminance is actually

    colour blind. Completely

    different part of the brain

    does luminance vs. colour.4

  • 8/10/2019 Weeks3-4 Jython

    5/98

    5

    School of Computer Science & IT

    Digitising pictures as bunches of little dots

    Our eyes effectively digitise pictures into lots of

    little dots

    Enough dots and it lookslike a continuous wholeto our eye

    Our eye has limited resolution Our background/depth acuityis particularly low

    Each picture element is referred to as apixel

  • 8/10/2019 Weeks3-4 Jython

    6/98

    6

    School of Computer Science & IT

    Pixels

    Pixels arepicture elements Each pixel object knows its colour

    It also knows where it is in itspicture

    When we zoom thepicture to 500%, we can

    see individual pixels.

  • 8/10/2019 Weeks3-4 Jython

    7/987School of Computer Science & IT

    A Picture is a matrix of pixels

    Its not a continuousline of elements, that is,

    an array A picture has two

    dimensions: Width andHeight

    We need a two-dimensional array: amatrix

    7

  • 8/10/2019 Weeks3-4 Jython

    8/988School of Computer Science & IT

    Referencing a matrix

    We talk about positionsin a matrix as (x,y), or(horizontal, vertical) or

    (column, row) Element (1,0) in the

    matrix at left is thevalue 12

    Element (0,2) is 6

    8

  • 8/10/2019 Weeks3-4 Jython

    9/989School of Computer Science & IT

    Encoding colour

    Each pixel encodes colour at that position in thepicture

    Lots of encodings for colour Printers use CMYK: Cyan, Magenta, Yellow, and blacK.

    Others use HSB for Hue, Saturation, and Brightness (also called HSVfor Hue, Saturation, and Value)

    Well use the most common for computers RGB: Red, Green, Blue

    9

  • 8/10/2019 Weeks3-4 Jython

    10/9810School of Computer Science & IT

    How much can we encode in 8 bits?

    Lets walk it through. If we have one bit, we can represent two patterns:0 and 1.

    If we have two bits, we can represent four patterns:00, 01, 10, and 11.

    If we have three bits, we can represent eight patterns: 000, 001, 010, 011, 100,

    101, 110, 111

    General rule: In nbits, we can have 2npatterns In 8 bits, we can have 28patterns, or 256

    If we make one pattern 0, then the highest value we can represent is 28-1, or

    255

  • 8/10/2019 Weeks3-4 Jython

    11/9811School of Computer Science & IT

    Bytes, Bits, Pixels and Words A word inside a computer is a unit of storage that

    contains a number of bytes. Eg a 64bit word = 8 bytes.

    A byte is always 8 bits

    A bit is a light that is on (1) or off (0). Because it can

    have two values only, it is a binary digit A byte can represent a number ranging from 0 to 255.

    Think of the amount of Red, Green and Blue.

    A pixel needs 3 bytes. One for each amount of RGB.

    Since 3 x 8 = 24, this is known as 24 bit colour or

    true colour. Multiplied out, it exceeds 16.7 million and

    is known as millions of colours

  • 8/10/2019 Weeks3-4 Jython

    12/9812School of Computer Science & IT

    Encoding RGB

    Each componentcolour (red, green, andblue) is encoded as a

    single byte Colours go from (0,0,0)

    to (255,255,255) If all threecomponents are the same,

    the colour is in greyscale

    (200,200,200) at (3,1) (0,0,0) (at position (3,0) in example)

    is black

    (255,255,255) is white

    12

  • 8/10/2019 Weeks3-4 Jython

    13/9813School of Computer Science & IT

    Is that enough?

    Were representing colour in 24= 3 8 . Thats 16,777,216 (224) possible colours

    Our eye can discern millions of colours, so its probablypretty close

    But the real limitation is thephysicaldevices: We dontget 16 million colour shades out of a monitor

    Some graphics systems support 32 bits per pixel May be more pixels for colour, or an additional 8 bits to

    represent 256 levels of translucence

  • 8/10/2019 Weeks3-4 Jython

    14/9814School of Computer Science & IT

    Size of images

    320 x 240

    image

    640 x 480

    image

    1024 x 768

    monitor

    24 bit colour 230,400

    bytes

    921,600 bytes 2,359,296

    bytes

    32 bit colour 307,200bytes

    1,228,800bytes

    3,145,728bytes

    14

  • 8/10/2019 Weeks3-4 Jython

    15/9815School of Computer Science & IT

    Reminder: Manipulating Pictures

    >>> file=pickAFile()

    >>> print file

    /Users/guzdial/mediasources/barbara.jpg>>> picture=makePicture(file)

    >>> show(picture)

    >>> print picturePicture, filename /Users/guzdial/mediasources/barbara.jpg height 294 width

    222

    15

  • 8/10/2019 Weeks3-4 Jython

    16/9816School of Computer Science & IT

    Whats a picture?

    An encoding that represents an image Knows its height and width (size)

    Knows its filename (name)

    Knows its windowif its opened (via showand repainted

    with repaint)

  • 8/10/2019 Weeks3-4 Jython

    17/9817School of Computer Science & IT

    Manipulating pixels

    >>> pixel=getPixel(picture,1,1) >>> print pixel

    Pixel, colour=colour r=168 g=131 b=105

    >>> pixels=getPixels(picture)

    >>> print pixels[0] Pixel, colour=colour r=168 g=131 b=105

    getPixel(picture,x,y)gets a single pixel.

    getPixels(picture)gets allof pixels in an

    array. (Square brackets is a standard array reference

    notationwhich well generally notuse.)

  • 8/10/2019 Weeks3-4 Jython

    18/9818School of Computer Science & IT

    Pictorially

    pixels [0]

    pixels [1]

    pixels [2]

    pixels [3]

    pixels [n]

    Where n is the

    number of pixels inthe picture

    Each box/byte contains

    a number between 0 and

    255

    which represents an R or

    G or B

    value

  • 8/10/2019 Weeks3-4 Jython

    19/9819

    QuickTime and aH.264 decompressor

    are needed to see this picture.

    School of Computer Science & IT

    Some JES explain oddities

  • 8/10/2019 Weeks3-4 Jython

    20/98

    20School of Computer Science & IT

    What can we do with a pixel?

    getRed, getGreen, and getBlue

    are functions that take a pixel as input

    and return a value between 0 and

    255 setRed, setGreen, and setBlue

    are functions that take a pixel as input

    and a value between 0 and 255

  • 8/10/2019 Weeks3-4 Jython

    21/98

    21School of Computer Science & IT

    We can also get, set, and make

    Colours getColourtakes a pixel as input and returns a Colour

    object with the colour at that pixel

    setColourtakes a pixel as input anda Colour, then setsthe pixel to that colour

    makeColourtakes red, green, and blue values (in thatorder) between 0 and 255, and returns a Colour object

    pickAColourlets you use a colour chooser and returns

    the chosen colour We also have functions that can makeLighterandmakeDarkeran input colour

  • 8/10/2019 Weeks3-4 Jython

    22/98

    22School of Computer Science & IT

    access to the picture tool

    To open the Picture Tool: Open a file using pickAFile().

    Create a picture (object) by usingmakePicture(fileName)or makeEmptyPicture().

    Run the openPictureTool(Picture)or select'Picture Tool...' from the MediaTools menu

    I havent been able to do it from the MediaTools menu.If you can, tell me how!

    How do you find out what RGB values you have?

  • 8/10/2019 Weeks3-4 Jython

    23/98

    23

    QuickTime and a

    decompressorare needed to see this picture.

    School of Computer Science & IT

    How do you find out what RGB values you have?

  • 8/10/2019 Weeks3-4 Jython

    24/98

    24School of Computer Science & IT

    Distance between colours?

    Sometimes you need to, e.g., when deciding if somethingis a close enough match

    How do we measure distance? Pretend its cartesian coordinate system Distance between two points:

    Distance between two colours:

  • 8/10/2019 Weeks3-4 Jython

    25/98

    25School of Computer Science & IT

    Demonstrating: Manipulating Colours

    >>> print getRed(pixel)

    168>>> setRed(pixel,255)

    >>> print getRed(pixel)

    255

    >>> colour=getColour(pixel)

    >>> print colourcolour r=255 g=131 b=105

    >>> setColour(pixel,colour)

    >>> newColour=makeColour(0,100,0)

    >>> print newColour

    colour r=0 g=100 b=0>>> setColour(pixel,newColour)

    >>> print getColour(pixel)

    colour r=0 g=100 b=0

    >>> print colour

    colour r=81 g=63 b=51>>> print newcolour

    colour r=255 g=51 b=51

    >>> print distance(colour,newcolour)

    174.41330224498358

    >>> print colourcolour r=168 g=131 b=105

    >>> print makeDarker(colour)

    colour r=117 g=91 b=73

    >>> print colour

    colour r=117 g=91 b=73>>> newcolour=pickAColour()

    >>> print newcolour

    colour r=255 g=51 b=51

    25

  • 8/10/2019 Weeks3-4 Jython

    26/98

  • 8/10/2019 Weeks3-4 Jython

    27/98

    27School of Computer Science & IT

    Use a loop!

    Our first picture recipe

    def decreaseRed(picture):

    for p in getPixels(picture):

    value=getRed(p)setRed(p,value*0.5)

    Used like this:

    >>> file="/Users/guzdial/mediasources/barbara.jpg">>> picture=makePicture(file)

    >>> show(picture)

    >>> decreaseRed(picture)

    >>> repaint(picture)

    27

  • 8/10/2019 Weeks3-4 Jython

    28/98

    28School of Computer Science & IT

    Our first picture recipe works for any picture

    defdecreaseRed(picture):

    forp ingetPixels(picture):

    value=getRed(p)

    setRed(p,value*0.5)

    Used like this:

    >>> file="/Users/guzdial/mediasources/katie.jpg"

    >>> picture=makePicture(file)

    >>> show(picture)

    >>> decreaseRed(picture)

    >>> repaint(picture)

  • 8/10/2019 Weeks3-4 Jython

    29/98

  • 8/10/2019 Weeks3-4 Jython

    30/98

    30School of Computer Science & IT

    Decreasing the red in a picture

    Recipe: To decrease the red

    Ingredients: One picture, name it pict

    Step 1: Get all the pixels of pict. For each pixel pin theset of pixels

    Step 2: Get the value of the red of pixel p, and set it to50% of its original value

  • 8/10/2019 Weeks3-4 Jython

    31/98

    31School of Computer Science & IT

    Use a forloop!

    Our first picture recipe

    defdecreaseRed(pict):

    allPixels = getPixels(pict)

    forp inallPixels:

    value = getRed(p)

    setRed(p, value * 0.5)

    The loop

    - Note the indentation!

  • 8/10/2019 Weeks3-4 Jython

    32/98

    32School of Computer Science & IT

    How forloops are written

    foris the name of the command

    An index variable is used to hold each of the different

    values of a sequence

    The word in

    A function that generates a sequence

    The index variable will be the name for one value inthe sequence, each time through the loop

    A colon (:)

    And a block (the indented lines of code)

    defdecreaseRed(pict):

    allPixels = getPixels(pict)

    forp inallPixels:

    value = getRed(p)

    setRed(p, value * 0.5)

  • 8/10/2019 Weeks3-4 Jython

    33/98

    33School of Computer Science & IT

    What happens when a forloop is executed

    The index variableis set to an item in the sequence

    The block is executed

    The variable is often used inside the block

    Then execution loopsto the forstatement, where

    the index variable gets set to the nextitem in the

    sequence Repeat until every value in the sequence is used.

  • 8/10/2019 Weeks3-4 Jython

    34/98

    34School of Computer Science & IT

    getPixelsreturns a sequence of pixels

    Each pixel (object)knows its colour andplace in the originalpicture

    Change the pixel,

    you change thepicture

    So the loops hereassign the index

    variablepto eachpixel in the picturepicture, one at atime.

    defdecreaseRed(picture):allPixels = getPixels(picture)

    forp inallPixels

    originalRed = getRed(p)

    setRed(p, originalRed * 0.5)

    defdecreaseRed(picture):

    forp ingetPixels(picture):

    originalRed = getRed(p)

    setRed(p,originalRed * 0.5)

    or equivalently

  • 8/10/2019 Weeks3-4 Jython

    35/98

    35School of Computer Science & IT

    Do we need the variable originalRed?

    No: Having removed allPixels, we can also do

    without originalRedin the same way: We can calculate the original red amount right when we are

    ready to change it. Its a matter of programming style. The meanings are the

    same.

    defdecreaseRed(picture):forp ingetPixels(picture):

    setRed(p, getRed(p) * 0.5)

    defdecreaseRed(picture):

    forp ingetPixels(picture):

    originalRed = getRed(p)

    setRed(p, originalRed * 0.5)

  • 8/10/2019 Weeks3-4 Jython

    36/98

    36School of Computer Science & IT

    Lets walk that through slowly

    Here we take a picture

    object in as aparameter

    to the function and call itpicture

    defdecreaseRed(picture):

    forp ingetPixels(picture):

    originalRed = getRed(p)

    setRed(p, originalRed * 0.5)

    picture

  • 8/10/2019 Weeks3-4 Jython

    37/98

    37School of Computer Science & IT

    Now, get the pixels

    We get all the pixels from

    the picture, then make p

    be the name of each one

    one at a time

    Pixel,

    colour

    r=135g=131

    b=105

    Pixel,

    colour

    r=133g=114

    b=46

    Pixel,

    colour

    r=134g=114

    b=45

    p

    getPixels()

    defdecreaseRed(picture):

    forp ingetPixels(picture):

    originalRed = getRed(p)

    setRed(p, originalRed * 0.5)

    picture

  • 8/10/2019 Weeks3-4 Jython

    38/98

    38School of Computer Science & IT

    Get the red value from pixel

    We get the red value of pixel

    p and name it originalRed

    originalRed= 135

    defdecreaseRed(picture):

    forp ingetPixels(picture):

    originalRed = getRed(p)

    setRed(p, originalRed * 0.5)

    picture

    Pixel,

    colour

    r=135g=131

    b=105

    Pixel,

    colour

    r=133g=114

    b=46

    Pixel,

    colour

    r=134g=114

    b=45

    p

    getPixels()

    38

  • 8/10/2019 Weeks3-4 Jython

    39/98

    39School of Computer Science & IT

    Now change the pixel

    Set the red value of pixel p

    to 0.5 (50%) of originalRed

    picturePixel,

    colour

    r=67g=131

    b=105

    p

    originalRed = 135

    defdecreaseRed(picture):forp ingetPixels(picture):

    originalRed = getRed(p)

    setRed(p, originalRed * 0.5)

    getPixels()Pixel,

    colour

    r=133g=114

    b=46

    Pixel,

    colour

    r=134g=114

    b=45

  • 8/10/2019 Weeks3-4 Jython

    40/98

    40School of Computer Science & IT

    Then move on to the next pixel

    Move on to the next pixel

    and name itp

    picture

    poriginalRed = 135

    defdecreaseRed(picture):

    forp ingetPixels(picture):

    originalRed = getRed(p)

    setRed(p, originalRed * 0.5)

    getPixels()Pixel,

    colour

    r=67g=131

    b=105

    Pixel,

    colour

    r=133g=114

    b=46

    Pixel,

    colour

    r=134g=114

    b=45

  • 8/10/2019 Weeks3-4 Jython

    41/98

    41School of Computer Science & IT

    Get its red value

    p

    Set originalRedto the red

    value at the new p, then

    change the red at that newpixel.

    p

    defdecreaseRed(picture):

    forp ingetPixels(picture):

    originalRed = getRed(p)

    setRed(p, originalRed * 0.5)

    picture

    originalRed = 133

    getPixels()Pixel,

    colour

    r=67g=131

    b=105

    Pixel,

    colour

    r=133g=114

    b=46

    Pixel,

    colour

    r=134g=114

    b=45

    41

  • 8/10/2019 Weeks3-4 Jython

    42/98

    42School of Computer Science & IT

    And change this red value

    Change the red value at pixel pto

    50% of value

    defdecreaseRed(picture):forp ingetPixels(picture):

    originalRed = getRed(p)

    setRed(p, originalRed * 0.5)

    pp

    picture

    value= 133

    getPixels()Pixel,

    colour

    r=67g=131

    b=105

    Pixel,

    colour

    r=66g=114

    b=46

    Pixel,

    colour

    r=134g=114

    b=45

    42

  • 8/10/2019 Weeks3-4 Jython

    43/98

    43School of Computer Science & IT

    And eventually, we do all pixels

    We go from this to this!

    43

  • 8/10/2019 Weeks3-4 Jython

    44/98

    44School of Computer Science & IT

    Tracing/Stepping/Walking through the

    program

    What we just did is called stepping or walking throughthe program

    You consider each step of the program, in the orderthat the computer would execute it

    You consider what exactly would happen You write down what values each variable (name) has

    at each point.

    Its one of the most important debuggingskills you can

    have.And everyonehas to do a lotof debugging, especially

    at first.

  • 8/10/2019 Weeks3-4 Jython

    45/98

    45School of Computer Science & IT

    Once we make it work for one picture, it will

    work for any picture

    45

  • 8/10/2019 Weeks3-4 Jython

    46/98

    46School of Computer Science & IT

    Think about what we just did

    Did we change theprogram at all?

    Did it work for all the

    different examples? What was the inputvariable pictureeachtime, then? It was the value of whatever

    picture we provided as input!

    def decreaseRed(picture):

    for p in getPixels(picture):

    value=getRed(p)

    setRed(p,value*0.5)

    NOTE: If you have a

    variablepicturein your

    Command Area, thatsnot the sameas the

    picturein decreaseRed.

  • 8/10/2019 Weeks3-4 Jython

    47/98

    47School of Computer Science & IT

    Read it as a Recipe

    Recipe: To decrease the red Ingredients: One picture, name it pict

    Step 1: Get all the pixels of pict. For each pixel pin

    the pixels

    Step 2: Get the value of the red of pixel p, and set itto 50% of its original value

    def decreaseRed(pict):

    for p in getPixels(pict):value=getRed(p)

    setRed(p,value*0.5)

  • 8/10/2019 Weeks3-4 Jython

    48/98

    48School of Computer Science & IT

    Lets use something with known red to manipulate:

    Santa Claus

    What if you decrease Santas red again and

  • 8/10/2019 Weeks3-4 Jython

    49/98

    49School of Computer Science & IT

    What if you decrease Santa s red again and

    again and again?

    >>> file=pickAFile()

    >>> pic=makePicture(file)

    >>> decreaseRed(pic)

    >>> show(pic)

    (Thats the first one)

    >>> decreaseRed(pic)

    >>> repaint(pic)

    (Thats the second)

    You can also use:

    explore(pic)

    to open the MediaTools

    49

  • 8/10/2019 Weeks3-4 Jython

    50/98

    50School of Computer Science & IT

    If you make something you like

    writePictureTo(picture,filename)

    Windows:writePictureTo(myPicture,"C:/mediasources/my-picture.jpg")

    MacOS or other forms of Unix:writePictureTo(myPicture, "~me/mediasources/my-picture.jpg")

    Writes the picture out as a JPEG

    Be sure to end your filename as .jpg!

    If you dont specify a full path, your file will be saved in the

    same directory as JES.

    I i R d Wh t h d h ?!?

  • 8/10/2019 Weeks3-4 Jython

    51/98

    51School of Computer Science & IT

    Increasing Red

    def increaseRed(picture):

    for p in getPixels(picture):value=getRed(p)

    setRed(p,value*1.2)

    What happened here?!?

    Remember that the limit for

    redness is 255.

    If you go beyond255, all

    kinds of weird things can

    happen if you have

    Modulo checked in

    Options.

    H d i R d diff f

  • 8/10/2019 Weeks3-4 Jython

    52/98

    52School of Computer Science & IT

    How does increaseRed differ from

    decreaseRed?

    Well, it does increase rather than decrease red, butother than that

    It takes the same input

    It can also work for anypicture

    Its a specification of aprocessthatll work for anypicture

    Theres nothing specific to a specific picture here.

    Cl i Bl

  • 8/10/2019 Weeks3-4 Jython

    53/98

    53School of Computer Science & IT

    Clearing Blue

    def clearBlue(picture):

    for p in getPixels(picture):

    setBlue(p,0)

    Again, this will work for any

    picture.

    Try stepping through this one

    yourself!You can do that by starting up the

    watcher, but its very tedious and

    slow, so dont bother.

  • 8/10/2019 Weeks3-4 Jython

    54/98

    54School of Computer Science & IT

    Can we combine these?

    Why not!

    How do we turn thisbeach scene into asunset?

    What happens atsunset? At first, I tried increasing the red, but

    that made things like red specks inthe sand REALLY prominent.

    That cant be how it reallyworks

    New Theory: As the sun sets, less blueand green is visible, which makesthings look more red.

    54

    A S t ti F ti

  • 8/10/2019 Weeks3-4 Jython

    55/98

    55School of Computer Science & IT

    A Sunset-generation Function

    defmakeSunset(picture):

    forp ingetPixels(picture):value=getBlue(p)

    setBlue(p,value*0.7)

    value=getGreen(p)

    setGreen(p,value*0.7)

    Li ht i d d k i i

  • 8/10/2019 Weeks3-4 Jython

    56/98

    56School of Computer Science & IT

    Lightening and darkening an image

    deflighten(picture):

    forpx ingetPixels(picture): colour = getColour(px)

    colour = makeLighter(colour)

    setColour(px ,colour)

    defdarken(picture):

    forpx ingetPixels(picture):colour = getColour(px)

    colour = makeDarker(colour)

    setColour(px ,colour)

    56

    C ti ti

  • 8/10/2019 Weeks3-4 Jython

    57/98

    57School of Computer Science & IT

    Creating a negative

    Lets think it through R,G,B go from 0 to 255

    Lets say Red is 10. Thats very light red.

    Whats the opposite? LOTS of Red!

    The negative of that would be 245: 255-10

    So, for each pixel, if we effectively negate eachcolour component in creating a new colour, we

    negate the whole picture.

    R i f ti ti

  • 8/10/2019 Weeks3-4 Jython

    58/98

    58School of Computer Science & IT

    Recipe for creating a negative

    defnegative(picture):

    forpx ingetPixels(picture):red=getRed(px)

    green=getGreen(px)

    blue=getBlue(px)

    negColour=makeColour(255-red,255-green,255-blue)

    setColour(px,negColour)

    58

  • 8/10/2019 Weeks3-4 Jython

    59/98

    59School of Computer Science & IT

    Programming Style

    The constant 255 is called a magic number Weknow what it means, but what about someone

    else?

    # MAX_COLOUR is the maximum integer we can represent in a byte

    MAX_COLOUR = 255

    defnegative(picture):

    forpx ingetPixels(picture):

    red=getRed(px)

    green=getGreen(px)

    blue=getBlue(px)

    negColour=makeColour(MAX_COLOUR-red, MAX_COLOUR-green, MAX_COLOUR-blue)

    setColour(px,negColour)

  • 8/10/2019 Weeks3-4 Jython

    60/98

    60School of Computer Science & IT

    Original, negative, negative-negative

    60

  • 8/10/2019 Weeks3-4 Jython

    61/98

    61School of Computer Science & IT

    Converting to greyscale

    We know that if red=green=blue, we get grey But what value do we set all three to?

    What we need is a value representing the

    darknessof the colour, the luminance There are lots of ways of getting it, but one way

    that works reasonably well is dirt simplesimplytake the average:

    61

    Converting to greyscale

  • 8/10/2019 Weeks3-4 Jython

    62/98

    62School of Computer Science & IT

    Converting to greyscale

    defgreyScale(picture):

    forp ingetPixels(picture):

    intensity = (getRed(p)+getGreen(p)+getBlue(p))/3

    setColour(p,makeColour(intensity,intensity,intensity))

    62

    Can we get back again?

  • 8/10/2019 Weeks3-4 Jython

    63/98

    63School of Computer Science & IT

    Can we get back again?

    Nope

    Weve lost informationWe no longer know what the ratios are between thereds, the greens, and the blues

    We no longer know any particular value.

    But thats not really the best greyscale

  • 8/10/2019 Weeks3-4 Jython

    64/98

    64School of Computer Science & IT

    But that s not really the best greyscale

    In reality, we dont perceive red, green, and blue asequalin their amount of luminance: How bright(or non-bright) something is.We tend to see blue as darker and red as brighter

    Even if, physically, the same amount of light is comingoff of each

    Photoshops greyscale is very nice: Very similar tothe way that our eye sees it

    B&W TVs are also pretty good

    Building a better greyscale

  • 8/10/2019 Weeks3-4 Jython

    65/98

    65School of Computer Science & IT

    Building a better greyscale

    Well weightred, green, and blue based on how

    light we perceive them to be, based on laboratoryexperiments.

    defgreyScaleNew(picture):forpx ingetPixels(picture):

    newRed = getRed(px) * 0.299

    newGreen = getGreen(px) * 0.587

    newBlue = getBlue(px) * 0.114

    luminance = newRed+newGreen+newBlue

    setColour(px,makeColour(luminance,luminance,luminance))

  • 8/10/2019 Weeks3-4 Jython

    66/98

    Lets use a black cat to compare

  • 8/10/2019 Weeks3-4 Jython

    67/98

    67School of Computer Science & IT

    Let s use a black cat to compare

    Average on left weighted on right

  • 8/10/2019 Weeks3-4 Jython

    68/98

    68School of Computer Science & IT

    Average on left, weighted on right

    68

    A different sunset-generation function

  • 8/10/2019 Weeks3-4 Jython

    69/98

    69School of Computer Science & IT

    A different sunset-generation function

    defmakeSunset2(picture):

    reduceBlue(picture) reduceGreen(picture)

    defreduceBlue(picture):

    forp ingetPixels(picture):

    value=getBlue(p)

    setBlue(p,value *0.7)

    defreduceGreen(picture):

    forp ingetPixels(picture):

    value=getGreen(p)

    setGreen(p,value *0.7)

    This one does the same thingas the earlier form.

    Its easier to read andunderstand: To make a sunset

    is to reduceBlue andreduceGreen.

    We use hierarchicaldecompositionto break downthe problem.

    This version is less inefficient,but thats okay.

    Programs are written forpeople, not computers.

    69

    Lets talk about functions

  • 8/10/2019 Weeks3-4 Jython

    70/98

    70School of Computer Science & IT

    Let s talk about functions

    How can we reuse variable names like pictureinboth a function and in the Command Area?

    Why do we write the functions like this? Wouldother ways be just as good?

    Is there such a thing as a better or worse function?

    Why dont we just build in calls to pickAFile and

    makePicture?

    One and only one thing

  • 8/10/2019 Weeks3-4 Jython

    71/98

    71School of Computer Science & IT

    One and only one thing

    We write functions as we do to make themgeneraland reusable

    Programmers like to re-use something theyve writtenbefore

    They write functions in a general way so that they can beused in manycircumstances.

    What makes a functiongeneraland thus reusable?

    A reusable function does One and Only One Thing

    Contrast these two programs

  • 8/10/2019 Weeks3-4 Jython

    72/98

    72School of Computer Science & IT

    Contrast these two programs

    def makeSunset(picture):

    for p in getPixels(picture):

    value=getBlue(p)

    setBlue(p,value*0.7)

    value=getGreen(p)

    setGreen(p,value*0.7)

    def makeSunset(picture):

    reduceBlue(picture)reduceGreen(picture)

    def reduceBlue(picture):

    for p in getPixels(picture):

    value=getBlue(p)

    setBlue(p,value*0.7)

    def reduceGreen(picture):

    for p in getPixels(picture):

    value=getGreen(p)

    setGreen(p,value*0.7)

    Yes, they have the same effect in the sense that

    makeSunset(somepicture)

    produces the same output greyscale picture

    Observations on the new makeSunset

  • 8/10/2019 Weeks3-4 Jython

    73/98

    73School of Computer Science & IT

    Observations on the new makeSunset

    Its okay to have more thanone function in the sameProgram Area (and file)

    makeSunsetin this one is

    somewhat easier to read. Its clear what it does

    reduceBlue andreduceGreen

    Thats important!

    def makeSunset(picture):reduceBlue(picture)

    reduceGreen(picture)

    def reduceBlue(picture):

    for p in getPixels(picture):

    value=getBlue(p)

    setBlue(p,value*0.7)

    def reduceGreen(picture):

    for p in getPixels(picture):

    value=getGreen(p)

    setGreen(p,value*0.7)

    Programs are written for people, not computers!

    Considering variations

  • 8/10/2019 Weeks3-4 Jython

    74/98

    74School of Computer Science & IT

    Considering variations

    We can only do this becausereduceBlueandreduceGreen, do one andonly one thing.

    If we put pickAFileandmakePicturein them, wedhave to pick a file twice (better

    be the same file), make thepicturethen save the pictureso that the next one could getit!

    def makeSunset(picture):

    reduceBlue(picture)

    reduceGreen(picture)

    def reduceBlue(picture):for p in getPixels(picture):

    value=getBlue(p)

    setBlue(p,value*0.7)

    def reduceGreen(picture):

    for p in getPixels(picture):value=getGreen(p)

    setGreen(p,value*0.7)

    Does makeSunset do one and only one

  • 8/10/2019 Weeks3-4 Jython

    75/98

    75School of Computer Science & IT

    Does makeSunset do one and only one

    thing?

    Yes, but its a higher-level, more abstractthing.

    Its built on lower-level one and only onething

    We call this hierarchical decomposition.

    You have some thingthat you want the computer to do?

    Redefine that thingin terms of smaller things

    Repeat until you know how to write the smaller things

    Then write the larger things in terms of the smaller

    things.

    Are all the things named picture the same?

  • 8/10/2019 Weeks3-4 Jython

    76/98

    76School of Computer Science & IT

    Are all the things named picture the same?

    What if we use this like this inthe Command Area:

    >>> file=pickAFile()

    >>> picture=makePicture(file)

    >>> makeSunset(picture)

    >>> show(picture)

    def makeSunset(picture):

    reduceBlue(picture)

    reduceGreen(picture)

    def reduceBlue(picture):for p in getPixels(picture):

    value=getBlue(p)

    setBlue(p,value*0.7)

    def reduceGreen(picture):

    for p in getPixels(picture):value=getGreen(p)

    setGreen(p,value*0.7)

    Local Variables: whats wrong here?

  • 8/10/2019 Weeks3-4 Jython

    77/98

    77School of Computer Science & IT

    Local Variables: what s wrong here?

    def main(): getName()

    print(Hello, name)

    def getName():

    name=input(Enter your name: )

    >>> main()

    Same local variables: Ok?

  • 8/10/2019 Weeks3-4 Jython

    78/98

    78School of Computer Science & IT

    Same local variables: Ok?

    def main():

    melbourne()

    jerusalem()

    def melbourne():

    birds = 5000

    print(melbourne has, birds, birds)

    def jerusalem():

    birds = 8000

    print(jerusalem has, birds, birds) >>>

    >>> main()

    Passing arguments

  • 8/10/2019 Weeks3-4 Jython

    79/98

    79School of Computer Science & IT

    Passing arguments

    def main():

    value = 5

    showDouble(value)

    def showDouble(number):

    result = number * 2

    print(result)

    >>>

    >>> main()

    Passing arguments: same name

  • 8/10/2019 Weeks3-4 Jython

    80/98

    80School of Computer Science & IT

    Passing arguments: same name

    def main():

    value = 5

    showDouble(value)

    def showDouble(value):

    result = value * 2

    print(value)

    >>>

    >>> main()

    Passing arguments

  • 8/10/2019 Weeks3-4 Jython

    81/98

    81School of Computer Science & IT

    Passing arguments

    def main():

    value = 5

    showDouble(value)

    print value

    def showDouble(value):

    result = value * 2

    print(value)

    >>>

    >>> main()

    Side effects?

  • 8/10/2019 Weeks3-4 Jython

    82/98

    82School of Computer Science & IT

    Side effects?

    def main():

    value = 5

    showDouble(value)

    print value

    def showDouble(number):

    number = number * 2

    print(number)

    >>>

    >>> main()

    What about now?

  • 8/10/2019 Weeks3-4 Jython

    83/98

    83School of Computer Science & IT

    What about now?

    def main():

    value = 5

    showDouble(value)

    print value

    def showDouble(value):

    value = value * 2

    print(value)

    >>>

    >>> main()

    Global Variables

  • 8/10/2019 Weeks3-4 Jython

    84/98

    84School of Computer Science & IT

    Global Variables

    These are to be used VERY sparingly

    You need to have a good reason to have this

    They are variables which are available to all

    functions Unlike function variables which die when the

    function ceases execution, these are alive during

    the entire time that the main program is executing

    These are defined outside any function, usually

    at the top

    Example with Global Variable

  • 8/10/2019 Weeks3-4 Jython

    85/98

    85School of Computer Science & IT

    p G

    my_value=10

    def show_value():

    print(my_value)

    >>>

    >>> show_value()

    Limitation of the previous example

  • 8/10/2019 Weeks3-4 Jython

    86/98

    86School of Computer Science & IT

    p p

    It only allows you to accessthe global variable

    What if I wanted to changethe global variable

    my_value=10 def show_value():

    my_value = my_value + 1

    print(my_value)

    >>>

    >>> show_value()

    Resultant error

  • 8/10/2019 Weeks3-4 Jython

    87/98

    87School of Computer Science & IT

    >>> ======= Loading Progam =======

    >>> show_value()

    The error was: 'my_value'

    Localname referenced but not bound to a value.

    A local name was used before it was created.

    You need to define the method or variable before

    you try to use it.

    Please check line 3 of /Users/isaac/Desktop/try

    A quick fix, but BAD programming style

  • 8/10/2019 Weeks3-4 Jython

    88/98

    88School of Computer Science & IT

    q , p g g y

    my_value=10

    def show_value():

    global my_value

    my_value = my_value + 1

    print(my_value)

    >>>

    >>> show_value()

    It prints 11

    What happens when we use a function

  • 8/10/2019 Weeks3-4 Jython

    89/98

    89School of Computer Science & IT

    pp

    When we type in the Command Area

    makeSunset(picture)

    Whatever object that is in (known) the Command Areavariablepicturebecomes the value of theplaceholder (input) variablepicture in

    def makeSunset(picture):

    reduceBlue(picture)

    reduceGreen(picture)

    makeSunsets picture is then passed as input to reduceBlueandreduceGreen, but their input variables are completely differentfrom makeSunsets picture.For the life of the functions, they are the same values (picture objects)

    Names have contexts

  • 8/10/2019 Weeks3-4 Jython

    90/98

    90School of Computer Science & IT

    In natural language, the same word has differentmeanings depending on context. Im going to fly to Vegas.

    Would you please swat that fly?

    A function is its owncontext. Input variables (placeholders) take on the value of the input values only for

    the life of the function

    (life means) Only while its executing Variables defined withina function also only exist withinthe context of that

    function

    The context of a function is also called its scope

    Input variables are placeholders

  • 8/10/2019 Weeks3-4 Jython

    91/98

    91School of Computer Science & IT

    Input variables are placeholders

    Think of the input variable as a placeholder

    like an empty cup waiting to be filled when called

    It takes the place of the input object

    During the time that the function is executing, theplaceholder variable stands forthe input object.

    When we modify the placeholder by changing itspixels with setRed, we actually change the input

    object.

    Variables within functions stay within

  • 8/10/2019 Weeks3-4 Jython

    92/98

    92School of Computer Science & IT

    y

    functions

    The variable valuein decreaseRedis created

    withinthe scope of decreaseRed That means that it only exists while decreaseRedis executing

    If we tried to printvalueafter running

    decreaseRed, it would work ONLYif wealready had a variable defined in the CommandArea The name valuewithin decreaseReddoesnt exist outside of that

    function

    We call that a localvariable

    def decreaseRed(picture):

    for p in getPixels(picture):

    value=getRed(p)

    setRed(p,value*0.5)

  • 8/10/2019 Weeks3-4 Jython

    93/98

  • 8/10/2019 Weeks3-4 Jython

    94/98

    Consider these two functions

  • 8/10/2019 Weeks3-4 Jython

    95/98

    95School of Computer Science & IT

    def decreaseRed(picture):

    for p in getPixels(picture):

    value=getRed(p)

    setRed(p,value*0.5)

    def decreaseRed(picture, amount):

    for p in getPixels(picture):

    value=getRed(p)

    setRed(p,value*amount)

    First, its perfectly okay to have multipleinputs to a function.

    The new decreaseRed now takes an input of the multiplier for the

    red value.

    decreaseRed(picture,0.5)would do the same thing

    decreaseRed(picture,1.25)would increasered 25%

    Names are important

  • 8/10/2019 Weeks3-4 Jython

    96/98

    96School of Computer Science & IT

    This function should probably be calledchangeRedbecause thats what it does.

    Is it more general? Yes.

    But is it the one and only one thing that youneed done? If not, then it may be less understandable. You can be too general

    def decreaseRed(picture, amount):

    for p in getPixels(picture):

    value=getRed(p)setRed(p,value*amount)

    Understandability comes first

  • 8/10/2019 Weeks3-4 Jython

    97/98

    97School of Computer Science & IT

    Consider these two functions below

    They do the same thing! The one on the right looks likethe other

    increase/decrease functions weve written. That may make it more understandable for you to write first.

    But later, it doesnt make much sense to you

    Why multiply by zero, when the result is always zero?!?

    def clearBlue(picture):

    for p in getPixels(picture):

    setBlue(p,0)

    def clearBlue(picture):

    for p in getPixels(picture):

    value = getBlue(p)setBlue(p,value*0)

    Always write the program understandable first

  • 8/10/2019 Weeks3-4 Jython

    98/98

    Write your functions so that youcan understandthem first Get your program running

    THENmake them better

    Make them more understandable to others Set to zero rather than multiply by zero

    Another programmer (or you in six months) may not remember

    or be thinking about increase/decrease functions

    Make them more efficient

    The new version of makeSunsettakes twice as long as the first

    version, because it changes all the pixels twice