< Return to Video

Image Code

  • 0:01 - 0:06
    In this section, we're gonna combine the
    earlier ideas of code with the ideas of
  • 0:06 - 0:12
    images and pixels and RGB numbers to sort
    of bring that together. Now, the examples
  • 0:12 - 0:17
    in this section, we'll just manipulate one
    pixel at a time. And, then in the next
  • 0:17 - 0:22
    section we'll scale it up to build, to do
    thousands of pixels at a time. So, to get
  • 0:22 - 0:27
    started, I wanna look at this, image
    called x.png. And this image is tiny, so I
  • 0:27 - 0:33
    can point to it here. So, it's a ten
    by ten image, it's there, shown on the
  • 0:33 - 0:38
    page. And it's a black image with a white
    X drawn on it. And, as I said, it's, it's
  • 0:38 - 0:44
    quite small. But we'll, we'll show it a
    little bit bigger in a second. So the PNG
  • 0:44 - 0:49
    is a, an image format, portable networks
    graphics. Just like, JPEG is a format
  • 0:49 - 0:55
    which you might be more familiar with. So
    those are both image formats. So, in this
  • 0:55 - 1:01
    case, what I wanna look at here.
    Is some code, that loads the x.png
  • 1:01 - 1:06
    image and displays it. So, this will just
    be a first, very simple example of code,
  • 1:06 - 1:10
    that works with images. So, here's the
    code in here, and I'll just talk about
  • 1:10 - 1:14
    what each line does. So, this first line,
    image = new SimpleImage(x.png)
  • 1:15 - 1:19
    What this does, is the
    right hand side essentially. Loads the
  • 1:19 - 1:24
    x.png image into memory. And we'll talk in
    more detail later on, what, what memory
  • 1:24 - 1:28
    is. But suffice to say, that's sort of
    the, it gets into the computer so it can,
  • 1:28 - 1:33
    the computer can work on it. So once I've
    got the image, the, the equal sign here
  • 1:33 - 1:37
    just stores it into a variable, which I'm,
    I'm gonna call image, just like, just like
  • 1:37 - 1:41
    we've seen variables before. The second
    line, image.setZoom(20). What that does is
  • 1:41 - 1:46
    it calls a, a set, the setZoom function,
    which is something that images have. And
  • 1:46 - 1:52
    it passes the number 20. And all this
    does. Is it such an option to show the
  • 1:52 - 1:57
    image at 20x size? And so. That's just
    something we'd use for a small image like
  • 1:57 - 2:01
    this just so it shows up big enough, that
    we can see it. And then finally, print(image),
  • 2:01 - 2:04
    is very similar to what we see
    before that, just prints the image over
  • 2:04 - 2:07
    the right hand side just as we saw,
    strings and numbers before. So we can all
  • 2:07 - 2:12
    just try it. So if I run this what you see
    is here's x.png, shows up over here. And
  • 2:12 - 2:17
    act-, you can actually count, one, two,
    three, four. You can actually count over,
  • 2:17 - 2:22
    and see it is in fact, ten pixels by ten
    pixels. And it's being displayed here at
  • 2:22 - 2:27
    20x size. So actually I could change
    this number here. So if I change this to a
  • 2:27 - 2:32
    ten, and then run it again then okay,
    well, now it's only 20X. And if I put
  • 2:32 - 2:35
    like, a, a 40 here, and run it,
    then okay, it a lot bigger. So I'll put it back to
  • 2:35 - 2:40
    twenty. So that's just a first
    example of a little bit of code, but we're
  • 2:40 - 2:45
    sort of going down the path of being able
    to load and manipulate images. Right, so
  • 2:45 - 2:50
    to make this a little more interesting. I
    wanna extend the code to be able to deal
  • 2:50 - 2:55
    with individual pixels so I'm a add a, a
    couple lines in the middle of the a
  • 2:55 - 2:59
    program here, so this line
    pixel=image.getPixel(0,0). What that
  • 2:59 - 3:04
    does it goes to the image and its gonna
    get a reference to a particular pixel
  • 3:04 - 3:10
    whatever, whenever x, y coordinates we
    specify here so 0,0 or this refers to the,
  • 3:10 - 3:15
    the upper left pixel, so it gets reference
    to the upper left pixel and stores that in
  • 3:15 - 3:20
    a variable pixel and then this line: pixel.setRed(255).
    That calls a function a
  • 3:20 - 3:26
    pixel has called setRed and what the, what
    setRed does is it takes in any number here
  • 3:26 - 3:31
    between the parentheses and whatever that
    number is, it takes it in and it sets the
  • 3:31 - 3:36
    red value of the pixel to be that number.
    So, I'm gonna run this. We need to see
  • 3:36 - 3:42
    what it does. And what you see is, what
    the code has done is obtained a reference
  • 3:42 - 3:46
    to this, the upper left pixel and it was
    black before and it, remember, recall
  • 3:46 - 3:52
    each, each pixel has the three numbers in
    it, red, green and blue. And so what this
  • 3:52 - 3:56
    code does, it went to the red number and
    it changed to 255, just overriding
  • 3:56 - 4:02
    whatever was there before. So when we see
    it, well okay it shows up as a red pixel,
  • 4:02 - 4:08
    so. There's a setRed to change the red
    values. There's an analogous function
  • 4:08 - 4:13
    setGreen and setBlue. So, we have these
    three, setRed, setGreen and setBlue.
  • 4:13 - 4:18
    And, so, with those, we can just change
    the red, green and blue values to be
  • 4:18 - 4:22
    whatever, wherever we want. So. Oh, and
    I'll mention it as an aside so there I, I
  • 4:22 - 4:25
    just, you know, introduced three
    functions. There's this separate page,
  • 4:25 - 4:29
    Image Functions Reference, that just lists
    all the functions in a table, so for some
  • 4:29 - 4:33
    later exercise, you might wanna, you can
    go see that if you want to remember what a
  • 4:33 - 4:37
    function does. But usually for the
    lectures I will just, if I'm gonna use a
  • 4:37 - 4:40
    function I'll just, as I'm going I'll talk
    about it. So, what I want to do to
  • 4:40 - 4:44
    demonstrate how, how these functions work,
    is just go through a bunch of examples.
  • 4:44 - 4:48
    Just use them to actually do something.
    Alright, so here are, so the, the format
  • 4:48 - 4:52
    of this is I've got, a little code area
    here with some starter code in it. And
  • 4:52 - 4:56
    then in this table down here, I've just
    listed a bunch of little, challenge
  • 4:56 - 5:00
    problems, like, oh, set something to be
    green or yellow or whatever, and we'll go
  • 5:00 - 5:04
    through them. For each one of these, on
    the right hand side there's this little
  • 5:04 - 5:08
    show button, so you can click that to see
    the solution code. So later on you can go
  • 5:08 - 5:12
    to this page yourself and the experiments
    I've tried you can just try yourself and
  • 5:12 - 5:15
    try variations of them or whatever.
    Alright, so let's try this first one. Well
  • 5:15 - 5:19
    actually, here, I'll, I'll run the code
    first to see what it does. Okay, so right
  • 5:19 - 5:24
    now it's just getting pixel (0,0) and
    setting it to red. So that, sort of seen
  • 5:24 - 5:29
    that before. Alright, so what's the first
    problem saying? Set pixel (0,0) to be
  • 5:29 - 5:34
    green. So the form here, is in English, it
    will say, well here's some effect we'd
  • 5:34 - 5:39
    like you to get and in sense the steps
    we're going through here to think about
  • 5:39 - 5:44
    well, what would be in the domain of code,
    in terms of function calls and numbers.
  • 5:44 - 5:49
    What are the series of operations we want
    to do to get that effect? So you're sort
  • 5:49 - 5:54
    of translating essentially from English
    into computer. So in this case it's said
  • 5:54 - 5:59
    to be set green. So what I want to do to
    do that, is instead of calling the setRed
  • 5:59 - 6:04
    function, I'll just change it to call setGreen. So lets try that. And there we go.
  • 6:04 - 6:09
    We gotta a green pixel instead. Lets try the next one. The next one
  • 6:09 - 6:15
    says set pixel (0,0) to be yellow. So right,
    well so, in order for the pixel to appear
  • 6:15 - 6:21
    yellow, what I want is for both the red
    and green values to be 255. You know,
  • 6:21 - 6:26
    yellow equals red plus green. So to do
    that, to change both the red and the
  • 6:26 - 6:30
    green. I'm gonna copy this line, and I'll
    paste it in here. And I'll just change
  • 6:30 - 6:34
    this one to red. So, I'm, I'm relying on
    the fact that, once I've got the reference
  • 6:34 - 6:38
    to pixel, I can do multiple things to it.
    So, on, on this first line, I call setRed,
  • 6:38 - 6:42
    I change the red value. And then I
    can call setGreen on the next line to
  • 6:42 - 6:46
    change the green. And it'll, the code will
    just go through and do each one of those
  • 6:46 - 6:51
    things internally. So let's try that. And
    sure enough, now I get yellow. So I've,
  • 6:51 - 6:54
    this sorta goes back to the idea that
    there is this pixel. It really just had
  • 6:54 - 6:59
    these three numbers in here. And here I'm
    writing code line by line to kinda reach
  • 6:59 - 7:04
    in there and change those numbers. Let's
    try the next one. Set pixel 1,0 to be
  • 7:04 - 7:10
    yellow. Where is that pixel? So, so that
    goes back to this line, the image.getPixel
  • 7:10 - 7:15
    line, which I haven't changed up
    until now. So the way, this works is,
  • 7:15 - 7:20
    whatever numbers I specify, 0,0,
    whatever, that's just a way of identifying
  • 7:20 - 7:25
    the different pixel inside here. So, if I
    say 1,0, that's gonna get the pixel
  • 7:25 - 7:29
    at x=1, y=0, so the convention is, it's x, then y. So, if
  • 7:29 - 7:34
    I run that, we can just see what it does.
    So, what you see is it's one over to the
  • 7:34 - 7:39
    right. So really, we could just specify
    anything over here. I could say, you know,
  • 7:39 - 7:43
    2,4. We'll see where that is, if I run
    it. Oh, okay, apparently that's over here.
  • 7:43 - 7:47
    So this goes back to what I was saying a
    couple of sections ago. That's it's,
  • 7:47 - 7:52
    that's x=0, that's x=1,
    that's x=2. We're not gonna play with
  • 7:52 - 7:56
    a lot of detail of messing with different
    x-y values, we just have to appreciate
  • 7:56 - 8:00
    that even if I have a million pixels here
    there is this x-y scheme where we could
  • 8:00 - 8:05
    dial it in with a particular x-y number
    to dial into exactly a particular pixel.
  • 8:05 - 8:14
    So text one says, set pixel 0,0 to be
    white. So I'll change this back, to be
  • 8:14 - 8:23
    pixel 0,0, so what do I do to red, green, blue to make it white? The answer is I
  • 8:23 - 8:30
    want to set all three values to 255. So
    notice, instead of say, re-typing pixel.,
  • 8:30 - 8:35
    whatever it is by hand, a lot of times I
    find it easier to copy an existing one and
  • 8:35 - 8:39
    then just edit it a little bit. So, I'm
    gonna put in a third call here
  • 8:39 - 8:44
    pixel.setBlue(255). So, the, the result of
    all three of these. Let's try it. Yeah,
  • 8:44 - 8:47
    sure enough, it sets it to be white. So
    I've set all three values to be
  • 8:47 - 8:51
    [inaudible]. So there's a couple more
    problems here. I'm actually not gonna
  • 8:51 - 8:55
    work, but, if you want to, you could go to
    this page, and try any number of
  • 8:55 - 8:59
    experiments or try those as well. And
    then, once you're comfortable with that
  • 8:59 - 9:03
    kind of material, then, we'll be ready for
    some, a, some exercises.
Title:
Image Code
Video Language:
English
duvin1 edited English subtitles for Image Code
duvin1 edited English subtitles for Image Code
duvin1 edited English subtitles for Image Code
stanford-bot edited English subtitles for Image Code
stanford-bot edited English subtitles for Image Code
stanford-bot added a translation

English subtitles

Revisions