Return to Video

Expressions

  • 0:00 - 0:05
    In this section, we're gonna add what are
    called expressions to the code we have
  • 0:05 - 0:10
    available. And that's kinda the last piece
    we needed to start writing, you know,
  • 0:10 - 0:14
    kinda realistic image manipulation code.
    And in particular by the end of this
  • 0:14 - 0:19
    section we'll play with puzzles that are
    based on images so, that'll be kinda fun.
  • 0:19 - 0:24
    So we've written code like this a lot of
    times. So, here it says print(42). We just
  • 0:24 - 0:28
    have a number 40 or 100 or 250, or
    whatever and it's just in the code. It
  • 0:28 - 0:33
    turns out you can instead write that this
    way. So it says print(11+31). And the
  • 0:33 - 0:37
    11+31 here, that's called an
    expression. And basically, instead of a, a
  • 0:37 - 0:43
    fixed number that's known ahead of time,
    we can put a little, a little arithmetic
  • 0:45 - 0:48
    expression. So in this case, 11+31,
    it's a little problem. The way this works
  • 0:48 - 0:53
    is that, when the computer runs, when it
    gets to this line. The first thing it's
  • 0:53 - 0:57
    gonna do, is, it is said to evaluate the
    expression. So it just picks out the
  • 0:57 - 1:01
    expression, and says, alright. Well, I
    need to work out what number this is. In
  • 1:01 - 1:05
    this case, it just does the addition, so,
    11+31, it's 42. So once the
  • 1:05 - 1:09
    expression's been evaluated down to a
    number of 42, then the code can continue,
  • 1:09 - 1:14
    and just use that number. So, in effect,
    this'll just print 42. And so anywhere in
  • 1:14 - 1:18
    the code where we would have had a number
    like 0 or 255 or 100 or something
  • 1:18 - 1:23
    instead we're gonna be able to put little
    arithmetic expressions to sort of embed a
  • 1:23 - 1:28
    little computation to compute what number
    we want to use in that. That's gonna
  • 1:28 - 1:33
    enable us to, to solve better problems. So
    we haven't talked about it up to now but
  • 1:33 - 1:39
    the pixel has these three extra functions.
    There's pixel.getRed() and what getRed
  • 1:39 - 1:44
    does is it, it's kind of the reverse of
    setRed. It retrieves the number out of
  • 1:44 - 1:49
    the pixel. So there is some red value; 0
    or 100 or something. This retrieves it
  • 1:49 - 1:54
    out. And there's also getGreen and getBlue. And so these are going to be very
  • 1:54 - 1:59
    natural to use in expressions to mess
    with the RGB values in a pixel. So
  • 1:59 - 2:04
    suppose what I want to do is double the
    red value of a pixel so if it's 50 I wanna
  • 2:04 - 2:09
    make it a 100 if it's a 100 I wanna make
    it a 200 or whatever. So here's some code
  • 2:09 - 2:13
    that does that so I'm gonna walk through
    this and this idea of making a relative
  • 2:13 - 2:18
    change to a pixel whatever it was change
    it to you know triple it or something like
  • 2:18 - 2:22
    that, that gonna be a much more realistic
    way to deal with the RGB values
  • 2:22 - 2:27
    of pixels. So let's walks with what this code
    does. So this quote is correct. It does
  • 2:27 - 2:32
    double the red value of a pixel. So what
    the first line does, is, it calls pixel.getRed().
  • 2:32 - 2:36
    So that's gonna retrieve the
    number out. And let's just say, in this
  • 2:36 - 2:41
    case, that the red value is 50. So,
    pixel.getRed(), is gonna retrieve the 50.
  • 2:41 - 2:45
    And then, here, we're using the equal
    sign, and the way we have before, to just
  • 2:45 - 2:49
    store that number in a variable. I'm, I'm
    just gonna call old, for the, the old
  • 2:49 - 2:54
    value. So basically, this just stores 50
    in old. So then, the next line says,
  • 2:54 - 2:58
    pixel.setRed(old "times" 2). So here, I'm
    using the expression. And remember, the
  • 2:58 - 3:03
    way it works is, when it gets to this
    line, the first thing it's gonna wanna do
  • 3:03 - 3:07
    is evaluate the expression. So it's gonna
    look at old times 2. Old, remember it's
  • 3:07 - 3:11
    just storing 50. So basically, this says
    50 times 2, it's gonna evaluate that,
  • 3:11 - 3:15
    come up with 100. So it evaluates the
    expression to get 100. And now that it has
  • 3:15 - 3:19
    that value, then it can go ahead and run
    the code. And so it says pixel.setRed,
  • 3:19 - 3:24
    essentially 100. So if you, you think
    through the whole sequence, basically this
  • 3:24 - 3:29
    pulls the 50 out, multiplies by 2 to get
    100, and then stores that back. So in
  • 3:29 - 3:35
    effect, it multiplies by two. Now. In
    reality this is the way we're gonna write
  • 3:35 - 3:39
    it. The whole thing can be condensed down
    to just one line because, really, that,
  • 3:39 - 3:43
    that variable old there was, it wasn't
    adding a lot. It was just kinda a
  • 3:43 - 3:47
    temporary holding spot. And so let's
    imagine that same case of pixel where the
  • 3:47 - 3:51
    red value is 50, and I'm just gonna run
    this. I've just condensed it down to one
  • 3:51 - 3:56
    line. So, let's imagine this code running.
    So, I have a pixel with actually here I
  • 3:56 - 3:59
    have it. Pixel with red of 50 and I'm
    gonna run this line. So the first thing
  • 3:59 - 4:04
    it's gonna do is it's gonna notice that
    there's an expression here, pixel.getRed()
  • 4:04 - 4:08
    times 2. So, it's gonna evaluate
    that expression. So pixe.getRed() is
  • 4:08 - 4:13
    gonna go get what the red value is
    currently. So let say 50. So it'll say,
  • 4:13 - 4:17
    it's the same math we had before so 50
    times two. That's 100. And so then with
  • 4:17 - 4:22
    that value 100, essentially it's gonna
    call pixel.setRed of 100 to put that
  • 4:22 - 4:27
    back. So it works out as the same dynamic
    we just went through. So it gets out the
  • 4:27 - 4:32
    value, multiplies by two and puts it back.
    Or, in effect, served in English, it
  • 4:32 - 4:37
    doubles the red value. So I'll do a bunch
    of examples that follow, sort of this
  • 4:37 - 4:44
    pattern. So what we're gonna see. As I'll
    start using this inside of loops as were
  • 4:44 - 4:51
    as previously we had to you know say zero
    or 250 I we had some fixed number but now.
  • 4:51 - 4:57
    We're gonna write code and this is the
    example I just did. Code like this where
  • 4:57 - 5:02
    we're going to change the red value based
    on what the red value was before. And so
  • 5:02 - 5:08
    the dynamic is, we'll have, you know, very
    often we'll say pixel.setRed and then
  • 5:08 - 5:14
    in the parenthesis we'll call pixel.getRed()
    and then have some arithmetic. So
  • 5:14 - 5:19
    in this case, this line doubles the red
    value or ultimately, this line. Calls
  • 5:19 - 5:24
    pixel.setRed of pixel.getRed()
    times 0.5. So it's getting the old value
  • 5:24 - 5:28
    and multiplying it times 0.5, or
    essentially dividing by two, so it's gonna
  • 5:28 - 5:33
    make it smaller and then put that back.
    So, that's gonna be the well, we'll follow
  • 5:33 - 5:38
    that pattern, a lot of times. So let me
    get to a real example, here. So suppose I,
  • 5:38 - 5:43
    I'm gonna take the flower's image. And
    let's say I wanna make it look kind of
  • 5:43 - 5:48
    more orange. So my strategy is gonna be,
    well, let's change the green value. Let's
  • 5:48 - 5:52
    just turn down the green. And so,
    mathematically, I'm gonna say, let's
  • 5:52 - 5:56
    change the green to be 75 percent of
    whatever it was. Now, in this example,
  • 5:56 - 6:01
    I've actually started the page with a no
    code in it. So I'm just gonna actually
  • 6:01 - 6:05
    type this in. So I'll say pixel, so, if I
    wanna change the green to be 75 percent of
  • 6:05 - 6:10
    what it was. And this is, you know, some
    of the example I just showed. I'll say
  • 6:10 - 6:14
    pixel.setGreen. You can also write
    this outside in so, I wanna call setGreen
  • 6:14 - 6:19
    and I want to put some kind of
    expression here, right so I want it
  • 6:19 - 6:24
    to compute a number that I want to put
    back for the green value. So the pattern
  • 6:24 - 6:28
    I'm gonna use here is I'll say pixel.getGreen(),
    I'm gonna get the old value and
  • 6:28 - 6:33
    then I can kinda, you know multiply times
    whatever. In this case I multiply times
  • 6:33 - 6:38
    0.75. So I'll change it to be sort of,
    three quarters of whatever it was. So lets
  • 6:38 - 6:43
    try that. Oh, okay. So that does
    make it, if you recall it was
  • 6:43 - 6:49
    sorta yellow. Actually if I put. 1.0 here.
    We can make no change. Right, so that's
  • 6:49 - 6:54
    when it was nice and yellow. So that's your 0.75 back here.
  • 6:54 - 6:58
    Then we get kinda a little more orange. I
    should say, for these examples. I'm gonna,
  • 6:58 - 7:02
    I'm gonna just take the time to type the
    code in. And you can do that if you want
  • 7:02 - 7:06
    to go back on this yourself. There's a
    little show solution button down here. So,
  • 7:06 - 7:10
    if you come to the review later. The
    solution code is available. But, I just
  • 7:10 - 7:14
    felt like practicing purposes. It's better
    to start with a blank screen. And then
  • 7:14 - 7:18
    really go through the process of typing
    the code in. So, all these examples will
  • 7:18 - 7:23
    follow that pattern. Let's try something a
    little bit more difficult. So this says
  • 7:23 - 7:30
    set red, green and blue each to be, 0.75
    of their original values, and then we'll
  • 7:30 - 7:37
    try 0.5 and 0.25. So what I'm gonna do. A
    great computer science tradition, instead
  • 7:37 - 7:43
    of typing the code in absolutely from
    scratch, I'm gonna copy it, the one I made
  • 7:43 - 7:50
    before and I'll just paste it. I'll make
    three copies of it, and then we'll fix it
  • 7:50 - 7:56
    to do what I want here. Okay, so usually I
    always go in the order, red, green, blue.
  • 7:56 - 8:02
    So I'm gonna say setRed. To getRed() and,
    what did it say to, 0.75. Alright, so
  • 8:02 - 8:08
    that's, that one's good. And then we'll
    say setGreen to getGreen() times 0.75. And
  • 8:08 - 8:14
    then we'll do blue last. So setBlue,
    pixel.getBlue(). So you can sort of see
  • 8:14 - 8:19
    the pattern here of the set and get being
    combined. That's fine. It's a very
  • 8:19 - 8:24
    workable pattern. So I'll run it that way.
    Yeah, It's a little bit subtle but what
  • 8:24 - 8:29
    this has done, is taken the original image
    and made it a little bit darker. Cuz if
  • 8:29 - 8:33
    you think about it, by multiplying 0.75
    we're sort of moving everything towards
  • 8:33 - 8:38
    zero, and obviously 0,0,0 is pure
    black, so we're kind of compressing down
  • 8:38 - 8:43
    that way. So let's try make it a little
    extreme. So, I'll multiply times 0.5 and
  • 8:43 - 8:48
    I'll run that. Oh, it's a little darker.
    And if almost by 0.25 so just a quarter of
  • 8:48 - 8:54
    the original values. Oh, then yeah, then
    it's getting quite dark. So, this shows
  • 8:54 - 8:59
    a good realistic sort of interesting use
    of this kind of scaling idea of using
  • 8:59 - 9:05
    setRed combined with getRed() and multiplying
    times some number to sort of play
  • 9:05 - 9:10
    with the values. Either scaling them up or
    scaling them down. Alright. So there's a,
  • 9:10 - 9:14
    a third problem here I, I'm gonna sorta
    skip over. This one's just for extra
  • 9:14 - 9:18
    practice, if you wanna come and try this
    one and it has a, it has a solution code
  • 9:18 - 9:22
    as well. So what I really want to do. Is
    work one of these five, ten, twenty
  • 9:22 - 9:28
    puzzles. So the idea with the five, ten,
    twenty puzzles is that there were some
  • 9:28 - 9:33
    image of a flower, piece of fruit or
    something and it's been modified and the
  • 9:33 - 9:39
    way it's been modified is that the red,
    green and blue values have all been divided by
  • 9:39 - 9:45
    either five, ten or twenty. So the values
    are way to small the images could be
  • 9:45 - 9:50
    really dark and the challenge. Is to
    multiply the red, green, and blue values
  • 9:50 - 9:55
    by either five, ten, or twenty.
    Essentially to kind of undo the darkening
  • 9:55 - 10:01
    to kinda get the image back. And so. Such
    as this it just comes down to some
  • 10:01 - 10:06
    experimenting and playing around a little
    bit to try and figure out how to get the
  • 10:06 - 10:10
    image back. So the number five is used
    once the number ten is used once the
  • 10:10 - 10:15
    number twenty is used once so basically
    you just have to figure out, which one
  • 10:15 - 10:19
    goes with which color. And I should say,
    you know there's only six possible ways to
  • 10:19 - 10:24
    have five, ten and twenty so there's
    really not that many to go through. The
  • 10:24 - 10:29
    way I think about it is you can imagine
    well maybe the five is first is on red and
  • 10:29 - 10:33
    so then there's either, five is first and
    then it's either five, ten, twenty. Or
  • 10:33 - 10:37
    five, twenty, ten. So if five is first
    there's only two possibilities and then
  • 10:37 - 10:41
    likewise if ten is first there's only two
    possibilities and if twenty is first
  • 10:41 - 10:44
    there's is only two possibilities so
    that's a way you can kind of organize
  • 10:44 - 10:49
    while your searching through this, alright
    so let me let me go back here. As I said
  • 10:49 - 10:56
    before and grab, a copy of my code so we
    don't start with nothing. Alright, so in
  • 10:56 - 11:01
    this case what we have is a, a banana.
    Actually, here, I'll, I'll comment these
  • 11:01 - 11:06
    lines out for a second, so we can just see
    what the image looks like with nothing. So
  • 11:06 - 11:11
    there is the puzzle image. And what it
    show-, I'll tell you. There is a yellow
  • 11:11 - 11:16
    banana, and it's on a background of dark
    red bricks. And in between the red bricks,
  • 11:16 - 11:21
    there's little bits of green moss that you
    can see. So if you fix the image, we
  • 11:21 - 11:26
    should be able to see all of those things.
    So to fix the image. What I wanna do here
  • 11:26 - 11:31
    is multiply. Lets, I'm just gonna... I'll
    just start with five, ten, twenty like
  • 11:31 - 11:36
    these. So let's say, well let's guess that
    the red needs to be multiplied by five,
  • 11:36 - 11:41
    the green needs to be multiplied by ten
    and the blue has to be multiplied by
  • 11:41 - 11:46
    twenty. Its just a guess. So, If I do
    that. Mm-hm, well, well that, that's
  • 11:46 - 11:50
    clearly wrong, right? The banana doesn't
    look quite right. And the bricks have this
  • 11:50 - 11:55
    sort of blue cast, so that's not good. So
    I'll, I'll stick with the assumption that
  • 11:55 - 12:00
    the five is first, though. And I'll try
    the other permutation. Alright, so, well
  • 12:00 - 12:06
    maybe it's five, twenty, ten. So I'll
    try it that way. Oh, and that's worse.
  • 12:06 - 12:11
    Alright. So, I don't think the
    five is first. When I tried the two ways
  • 12:11 - 12:21
    with five first. So let's try the ten
    first, so I'll try ten. 520. Alright, so
  • 12:21 - 12:26
    I'm just scaling these up. Ew, hm. Well, I
    mean I think we're getting there, right? I
  • 12:26 - 12:31
    mean the banana looks pretty good but the
    bricks obviously are, that's not, you
  • 12:31 - 12:36
    know, that's purple, they're
    supposed to be red. So let me try, I'm
  • 12:36 - 12:42
    gonna try leaving the five in the middle
    and just putting the twenty first. Twenty,
  • 12:42 - 12:48
    five, ten, let's try that. There we have
    it. There's the banana, it looks nice and
  • 12:48 - 12:53
    yellow. You can see the bricks have this
    dark red color. And then even little bits
  • 12:53 - 12:58
    of, green moss here. That's sort of
    reassuring. So I'm gonna zoom in on this,
  • 12:58 - 13:04
    so I'll show you. [inaudible] some, some
    of the qualities of it. So partly there's,
  • 13:04 - 13:08
    there's the brick. And the moss and the
    bananas all look good. There's just a
  • 13:08 - 13:13
    minor thing but you'll see there's kind of
    a, see there's sort of this horizontal
  • 13:13 - 13:17
    banding. In the banana and that's okay
    that is that's the way the solution looks
  • 13:17 - 13:23
    when you do it quickly. That's happening
    because when the red, green, and blue when
  • 13:23 - 13:28
    they were divided by this numbers by ten
    or maybe twenty all of those values which
  • 13:28 - 13:33
    normally range to zero to fifty five they
    are compressed down to ranging maybe just
  • 13:33 - 13:38
    zero to twelve or zero to twenty four. And
    as a result... Because they were
  • 13:38 - 13:42
    compressed down to that range, there were
    just a few shades of yellow available,
  • 13:42 - 13:46
    when it was compressed down like that. And
    even when we expand it back up, we're
  • 13:46 - 13:50
    still stuck with just those few shades. So
    that's what we're seeing in these
  • 13:50 - 13:54
    horizontal band here, is, there, there
    were just a few different shades of
  • 13:54 - 13:58
    yellow. And so it's unable to capture.
    I'll zoom in a little bit. It's unable to
  • 13:58 - 14:03
    capture. The, the real gradations that you
    would want. And so that's just sort of a,
  • 14:03 - 14:07
    an artifact of the, the way this exercise
    works. Alright. So the, the exercises to
  • 14:07 - 14:12
    follow this section are actually just more
    of these five, ten, twenty puzzles. So,
  • 14:12 - 14:14
    it's something you should check out.
Title:
Expressions
Video Language:
English
duvin1 edited English subtitles for Expressions
duvin1 edited English subtitles for Expressions
duvin1 edited English subtitles for Expressions
stanford-bot edited English subtitles for Expressions
stanford-bot edited English subtitles for Expressions
stanford-bot added a translation

English subtitles

Revisions