CS 124
Fall 2023

Problem Set 10 FAQ

Problem Set 10 FAQ

If you don’t see your question here, post it on Piazza or come to office hours! See the links in the navigation bar for both of those options.

General questions

  1. Do we need to include docstrings in all of our functions?

    Yes.

  2. How can a function return more than one value?

    The easiest way to do this is to just return a list of values. Suppose I need a function that returns a string and a number. It could look something like the following:

    def hi():
        str = "hi"
        n = 2
        return [str, n]
    

    And you could call it and use the return value as follows:

      res = hi()
      x = res[0]   # x is "hi"
      y = res[1]   # y is 2
    

    Note that you aren’t required to write a function that returns multiple values in this problem set, but you are welcome to do so in one of your helper functions for the last problem.

Problem 1

  1. When I test one of my image-processing functions, I get an error message indicating that an index is out of bounds. What am I doing wrong?

    You are trying to access a pixel using a row and/or a column index that is either too big or too small.

    For example, for an image that is only 10 pixels wide and 10 pixels tall, trying to set a pixel at row 15 and column 30 would produce an error message.

    If you see this error, try to determine why your expressions for the row and/or column indices are incorrect.

    It may also help to remember that row and column indices start from zero, just like list indices. In other words, a 10 by 10 image does not have pixels at row 10 nor column 10.

  2. The image my program outputs for flip_vert has bright green pixels. What could be going wrong?

    As stated in the problem set, whenever a new image is created, it is filled with green pixels by default. If one or more pixels in the new image is never replaced by any other pixel value using the set_pixel() method, the pixels will remain green and be saved to the output file.

    A common reason for this issue is setting the dimensions of the new image to incorrect values. Make sure the dimensions are correct.

    This can also happen if any loops in your program do not “reach” row and column indices that are necessary to solve the problem.

  3. My flip_vert function is giving me the original image, rather than the flipped version. Why is this happening?

    The last line of each of your image-processing functions should save an image object to a file. In the invert function, we did this as follows:

    img.save(new_filename)
    

    In invert, this line works because we modified the pixels in the original image object img, and thus we want to save the modified version of img. This approach will also work for some of the functions that you need to write (e.g., grayscale()).

    However, for flip_vert you should be creating a new image object called new_img, and you need to make sure that you’re saving this new image object, not the original one. You can do this by making the following call

    new_img.save(new_filename)
    
  4. When I move a pixel in flip_vert, will I need to create a new set of RGB values?

    In flip_vert, you are simply moving pixels around from where they were located in the original image to where they should be located in the new one. As a result, you don’t need to change the RGB values of the pixel. Instead, you just need to put those RGB values at a different location in your new image.

    To make this clear, suppose I just want to make an identical copy of an entire image. Assume that I have two variables: img, which is my original image; and new_img, which is the new one. All I want to do is copy every pixel from img to new_img. In order to do this, I can use the following nested for loops:

    for r in range(img.get_height()):
        for c in range(img.get_width()):
            rgb = img.get_pixel(r, c)     # get the RGB values for the pixel at r,c
            new_img.set_pixel(r, c, rgb)  # use those same RGB values in the new image at r,c
    
  5. When I need to move a pixel in flip_vert, how do I determine where it should go?

    We recommend making a table that lists what should happen in concrete cases, and then come up with a formula that is based on that table.

    If the height of the image is h, here is what the beginning of this table should look like:

    row r in original     row in flipped
    -----------------     --------------
            0                 h - 1
            1                 h - 2
            2                 h - 3
    

    Given the pattern that you see in this table, what is a general formula for where a pixel from row r should go in the flipped image?

  6. How do I vertically mirror an image?

    In this problem, we want to mirror the top half of the image over the bottom half. When you are creating your new image, which pixels are going to change from the original image? How can you adjust the range of your nested loop so that you only change those pixels?

  7. How do I horizontally mirror an image?

    In this problem, we want to mirror the left half of the image over the right half. When you are creating your new image, which pixels are going to change from the original image? How can you adjust the range of your nested loop so that you only change those pixels?

  8. How should I start extract?

    For the reduce function from Part I we recommended using your nested loops to go through each pixel in the new image and copy the appropriate pixel from the old image. This approach will be useful here as well.

    Answering the following questions should get you started.

    • What are the height and width of the new image that extract will produce?

    • Which pixel from the old image will go at 0,0 (top left corner) in the new image?

    • Which pixel from the old image will go at r,c for any row r or column c in the new image?