Homework 5: Optical Illusions Due Friday November 3, 11:59PM EST

For this homework assignment, you will be using MiddImage to create images of optical illusions. We will begin with three functions that will be building blocks for your illusions. Then, you will choose an optical illusion to create. Each option includes a short reflection that will allow you to analyze the code that you wrote.

DateTasks to Complete
10/28Read through the assignment and complete problem 1, passing problem 1 tests on the autograder
10/29Complete problem 2, passing problem 2 tests on the autograder
10/30Complete problem 3, passing problem 3 tests on the autograder
10/31Start working on problem 4.1, pass problem 4.1 test on the autograder (NOTE: this is an easy test to pass, this problem is mostly manually graded)
11/01Complete problem 4.1 – 4.2, passing problem 4,2 tests on the autograder
11/02Complete problem 4.3 (reflection)
11/03DEADLINE! Finsh debugging and submit your final code. Attend drop-in hours if you’ve had problems with any of the previous parts. Make sure to review your code for code quality.

These are soft deadlines that are not part of your grade, but I encourage you to stick to this timeline if you’ve struggled to complete the homework assignments on time. Being ahead of this timeline is great!

Downloading starter files

You’ll need to download three files for this assignment and place them in the same directory.

  • hw5.py: This is where you will write all of the code for this assignment.
  • middimage.py: This is the MiddImage module. You should not edit this file!
  • colors.py: This module includes colors that you may import. You should not edit this file!

Installing Numpy + Pillow

In order to use middimage.py, you need to install two packages called numpy and Pillow. Go back to the instructions for lab 5 if you don’t remember how to install a package. When prompted for a package name, type numpy rather than pytest. Do the same thing for Pillow.

Submitting

I strongly recommend that you submit to gradescope at the end of each part to get feedback. You should submit hw5.py to the autograder. Do not submit any of the other files.

Problem 1: Straight Line

Your first task is to create a function that draws a straight line. You should return a copy of the im that includes a line with length length starting at position (x, y). If horizontal is True, the line should be horizontal; otherwise, it should be vertical. The color of the line should be the color indicated by rgb_list.

def straight_line(im, x, y, length, horizontal, rgb_list):
    """
    Your docstring goes here!
    """
    # your code goes here!

To create this function, you may re-order the following lines and add indentation:

for i in range(length):
if horizontal:
im2 = im.copy()
im2[y, x + i] = rgb_list
im2[y + i, x] = rgb_list
def straight_line(im, x, y, length, horizontal, rgb_list):
return im2
else:

You should update exactly length pixels with your function!

Assumptions:

  1. You may assume that length $\geq 1$
  2. You may assume that the line defined by x, y, length, and horizontal fits within im

There is code at the bottom of the hw5.py file that you downloaded that calls your functions and saves image files for you to see if your code works. Look for these image files in the same folder that your hw5.py file is stored in. If you want to call the functions yourself, here’s a code snippet to get you started:

im = mi.new(200, 200)
im2 = straight_line(im, 0, 100, 100, True, WHITE)
im2.show()

WHITE (along with a bunch of other colors, including BLACK, GREY, all of the primary colors, and all of the secondary colors) is imported at the top of your file from the colors module, which you downloaded!

You should submit to the autograder to check that your function is working properly before moving on to the next part.

Problem 2: Filled in Rectangle

Your second task is to create a function called filled_rectangle that creates a filled in rectangle.

def filled_rectangle(im, x0, y0, x1, y1, rgb_list):
    """
    Your docstring goes here!
    """
    # your code goes here!

Your rectangle should be inclusive of x0, y0, x1, and y1. You should return a copy of im that contains the rectangle.

HINT: If you choose to, you may use your straight_line function to create your filled in rectangle. Be careful with the arguments that you use!

Assumptions:

  1. You may assume that x0 $<$ x1 and y0 $<$ y1
  2. You may assume that the rectangle defined by x0, x1, y0, and y1 fits within im

See the image below for an example if you are stuck!

You should submit to the autograder to check that your function is working properly before moving on to the next part.

Problem 3: Filled in Circle

Your next task is to create a filled in circle.

This function should take five arguments:

  1. im, the image to modify.
  2. x, int, the horizontal coordinate (column) of the center of the disk.
  3. y, int, the vertical coordinate (row) of the center of the disk.
  4. radius, int, the radius of the disk.
  5. color, list, the color of the disk.
def filled_circle(im, x, y, radius, rgb_list):
    """
    Your docstring goes here!
    """
    # your code goes here!

You should return a copy of im that contains the circle.

HINT: your solution will likely involve nested loops

HINT: You may find it useful to recall the inequality that defines a disk in the plane. You can use the “open disk” or “closed disk” formula for this problem.

Assumptions:

  1. You may assume that the circle defined by x, y, and radius fits within im

See the image below for an example if you are stuck!

You should submit to the autograder to check that your function is working properly before moving on to the next part.

Part 4: Create an Illusion

For this part of the assignment, you will create a function that draws an optical illusion. You can choose any of the three illusions listed below:

Click on the link for the illusion that you would like to create for the complete instructions! It is worth noting that while different students may have different experiences, these illusions are listed approximately in order of difficulty. This is an opportunity to be creative and “choose your own adventure”, but if you don’t have a lot of time, I’d recommend the gradient illusion.

This part of the assignment is largely manually graded. The only autograded part simply checks that you return an updated copy of im with the same dimensions from your function. That means that you can be as creative as you want - feel free to play around with colors, etc.!

Submission

Submit your assignment on gradescope. The straight line, filled in rectangle and filled in circle are autograded; the illusion is graded manually. Feel free to be as creative as you want with colors, etc.

Credits

This assignment is adapted from Faan Tone Liu’s optical illusions assignment.