Lecture Materials

Questions & Answers


Q: When and how will we get our diagnostic grades?

A1:  live answered


Q: Was there an error in the code for the Karel question? I think the program that was supposed to be turn_around was named turn_left

A1:  Yep - we won’t deduct based on this, though.


Q: How did he get that data?

A1:  In 106A that quarter, we had a tool that provided snapshots of students as they were writing code so we could provide them feedback on their process!


Q: Did Chris say he wrote the algorithm for instagram filters?

A1:  No :) He’s going to show you one of the algorithms that Instagram also uses!

A2:  That would have been awesome, though

A3:  No. He said he didn't use Instgram for sepia filtering. He's writing his own algorithm. But Instragram was started by Stanford alums!


Q: Is there lecture code for this lecture? When I try to download, I’m navigated to an object not found page.

A1:  I’ll look into it

A2:  Fixed!


Q: what does the second line in the main function do?

A1:  It loads the image into an image file


Q: stylistically are those two pixel grab functions the same?

A1:  Unless you need the coordinates, you can use the former


Q: whats up with the “image/ “ part of simpleimage?

A1:  The images are in a directory called "images". So adding "images/" before the file name let's Python know that the image is an directory called "images".

A2:  The image is in a folder called ‘image/‘


Q: how can the pixel variable used in function sepia_pixel() able to modify pixel information of the image in main()?

A1:  You'll see more on this soon. But pixels are actually something you can change in a function and the change will persist.


Q: Is this the same idea for black and white (greyscale) images too? Are there exact values to multiply by for that type of filter?

A1:  In general, greyscale is made by setting R, G, and B to the average of the three components

A2:  Yes, same idea for greyscale. A simply way is to average over the RGB value for the pixels. A slightly more robust way is given in the files from Lecture 9 last week.


Q: where is that folder? how do you acess images from different places?

A1:  It’s in the PyCharm project - try downloading the lecture code to see where it!


Q: is it wrong to consider this double for loop basically as an area function?

A1:  You are basically looping over the area of the image, yes.


Q: in the sepia example, is the 'images/' the folder in which the image is saved? So if my image is in a nested folder, will my code have to say 'folder1/folder2/folder3' instead?

A1:  Yep!


Q: what are we importing on the line "import ..."?

A1:  You are importing a library called "tkinter". There more information about it in the handout on Graphics that was released today.

A2:  Check out the lecture code


Q: Are these canvas commands part of simpleimage or are they python commands?

A1:  They’re part of a library called tkinter which you can import like simpleimage


Q: how does it already know what “blue” is?

A1:  It’s in the tkinter library, which we’re importing at the top!


Q: what happened to the if __name__ == “main()” part?

A1:  It’s just not visible right now, it’s still there


Q: Do we need to import any library to use make_canvas?

A1:  Yes, it’s called tkinter

A2:  Yes. You'll need to import tkinter.


Q: Is make_canvas a preexisting function?

A1:  Yeah, you won’t need to write it


Q: How would we encode a named argument into a function?

A1:  We’ll get there later in the class!


Q: Is canvas the name of the variable where the rectangle is stored?

A1:  Yep, the canvas knows everything that’s drawn on it


Q: Black outline by default? How can you change the color of the line?

A1:  Black outline by default? How can you change the color of the line?

A2:  We’ll get there soon!


Q: where can I find defaults for all these graphical functions?

A1:  Check out the graphics reference we just posted


Q: could you do fill = custom color and create a color with an RGB value that you specify?

A1:  Yes. But see the graphics handout. There's a reference to a huge list of predefined named colors.

A2:  Yes, I believe so!


Q: Doesn’t the rectangle that we make end up being a different size in comparison to the original canvas? Would we still need to make the original canvas before creating the rectangle?

A1:  The canvas is measured in pixels as are the coodinates of the rectangle. Rectangles (and other shapes) apprear on a canvas. So you have to create the canvas first.

A2:  You specify the size of the rectangle in pixels, so it doesn’t matter what the size of the containing canvas is. Does that answer your question!


Q: How does canvas.mainloop() maintain the file? In terms of the instructions it tells the computer.

A1:  Can you clarify what you mean?

A2:  This command lets the canvas know that it should be rendering the shapes that were created in it. And it maintains the drawing (in case the graphics window moves for example). The full internals are beyond the scope of this class.


Q: what happens if we put something after canvas.mainloop()?

A1:  It doesn’t execute until the canvas is closed


Q: are some of these graphics principles used in photoshop? as in when it autodetects people or leaves etc?

A1:  Good question! Those algorithms look a little more like what we talked about on Friday - sto by OH if you want to learn more about how they work!


Q: Is the bottom right corner of the canvas (canvas width-1, canvas height -1)?

A1:  That’s the coordinate of the bottom right pixel, yes!


Q: so the default outline is black?

A1:  Yep


Q: can we assign these to be objects? like oval= create.oval()

A1:  Yes, these functions return identifiers for the graphical objects


Q: are there other way to define an oval?

A1:  You could write functions to do that if you have something specific in mind.

A2:  Do you have anything particular in mind?


Q: canvas.create… is a variable from the library, right? So, the program knows what to do?

A1:  Yep


Q: do the ‘ or “ matter

A1:  Nope


Q: Remind me what is “anchor=“w””?

A1:  The (x, y) coordinate is the left side of the text


Q: Just wondering if there are other predefined functions for that. Thanks

A1:  create_oval is the most common way


Q: Why is the image of simba on the bottom left if you entered ‘nw’, not the top left?

A1:  the 'nw' stands for "north west", which means the top-left corner is the place your are specifying with you x,y coordinates.

A2:  The anchor is which part of the image is at the coordinate. In this case, (0, 300) is the northwest (top left) of the image of simba


Q: when you run a program and it finishes, does it delete all the stacks we talked about earlier or can I access that canvas in this case later?

A1:  The stack frames are cleared from your memory


Q: Does w in anchor stand for west of anchor?

A1:  West of the text


Q: So if we want to right in arabic for example we would change anchor ?

A1:  Good question! I think so


Q: what are the alternatives to ‘w’

A1:  ‘w’ is West and all of the cardinal directions, and the diagonal ones, are options


Q: what about the size of the text?it can be change with the x,y position?

A1:  Nope, there are other parameters you can specify otherwise


Q: Why is the anchor always w?

A1:  It makes your life easier :)


Q: what does "from PTL import Image" do?

A1:  Imports the Pillow Library into your program


Q: oh, christ hasnt worked on that, right?

A1:  No


Q: I love the new 'example' section! Will you also put examples from previous classes? (e.g. the burrito one)

A1:  Yeah, we’ll get some more up there


Q: how does python actually draw? assigning pixel colors left to right? zig zag?

A1:  That’s actually up to your screen to decide! I think under the hood, Python will just set the pixels in whatever order is easiest and then your screen will refresh to show the new canvas.


Q: why do x + size and y + size rather than just size alone

A1:  You want to specify the x,y location of the lower right corner of the square, which is at x + size, y + size since size if the size of the square (not the absolute coordinate of the lower left corner).

A2:  To build suspense :)


Q: Why are we doing x+ SIZE and not just x?

A1:  To build suspense :)


Q: Can you explain what SIZE is?

A1:  The number of pixels high/width that each square should be.


Q: what’s the canvas paramenter in draw_square(canvas, col) doing?

A1:  draw_square needs the canvas as a parameter to draw on it


Q: Why is SIZE = CANVAS_SIZE / N_ROWS - 1 ?

A1:  live answered


Q: What’s the point of the -1 on the SIZE constant formula

A1:  live answered


Q: refresher on why we don’t have to define col?

A1:  The for loop defines it for you


Q: Is there a reason our SIZE is not 800/8?

A1:  live answered


Q: Just wondering is there any particular reason why we need to minus 1 when calculate size? To not overlap blocks?

A1:  live answered


Q: do we have to multiply by the size of the entire canvas, then?

A1:  No. The canvas is large enough to hold all the squares.


Q: are col and row already in canvas or they are created

A1:  They’re defined in the for loop on lines 20 and 23


Q: does the for x in range(y) define x as a new variable in terms of y?

A1:  No, it’s just a new variable that starts off at 0 and goes up until y - 1


Q: Is the canvas the size of the whole board?

A1:  There’s like a tiny bit of padding on the borders, but basically, yeah


Q: isnt the print missing? Because the last line says return only

A1:  You don’t need to print the color


Q: Can we finish assignment3 after this lecture ?

A1:  Yep


Q: ohhh, i think he answered, but can you explain further, please :( ?

A1:  get_color doesn’t need to print colors to the terminal


Q: in_to_color is built in?

A1:  No, Chris, wrote it


Q: Sorry just to clarify, when returning black and white, it is built in to color them?

A1:  live answered


Q: WOW whats a function that goes both ways to make 3 numners into 1???????

A1:  live answered


Q: So do you make the canvas before you use the whole canvas?

A1:  Yep!


Q: WOW! that’s so cool!!!

A1:  live answered


Q: I want to test out ghost.py with my own pictures how could I do that? I added them into a folder on pycharm but it says list index out of range

A1:  live answered


Q: Why don’t you have to specify tkinter.make_canvas()? Does python just know?

A1:  live answered