#!/usr/bin/env python # coding: utf-8 # In[ ]: from simpleimage import SimpleImage # As a reminder, here are the image functions we learned about in class. # # ``` # # Create image: # image = SimpleImage.blank(400, 200) # create new image of size 400, 200 # # # Access size: # w = image.width # h = image.height # # # Get pixel at x,y (0,0 is the top left corner): # pix = image.get_rgb(x, y) # # pix is an RGB list like [100, 200, 0], where the color components of the # # pixel are Red = 100, Green = 200, and Blue = 0. # # # Set pixel at x,y: # image.set_rgb(x, y, r, g, b) # # # Show image on screen: # image.show() # # # Show a small image zoomed-in (larger) so it's easier to see the pixels: # image.show(resize_width=200) # ``` # # ### Question 1 # # Let's create a blank image with a white background, that is an 11 by 11 square. # In[ ]: size = 11 # ### Question 2 # Now, let's set the middle pixel to a black dot by using the `set_rgb()` function. # In[ ]: # ### Question 3 # # After painting the center pixel of the picture black, let us paint the four corners black. # # It should look something like this. # # ![](four_dots_and_middle_dot.png) # In[ ]: # ### Question 4 # # Define a function called `draw_horizontal(img, x, y,length)` that will draw a horizontal line given an image, starting pixel, ending pixel and its length. # In[ ]: image = SimpleImage.blank(size, size) def draw_horizontal(img,x,y,length): # YOUR CODE GOES HERE pass # ### Question 5 # Define a function called `draw_vertical(img, x, y,length)` that draws a vertical line given an image, starting pixel, ending pixel and its length. # In[ ]: image = SimpleImage.blank(size, size) # YOUR CODE GOES HERE # ### Question 6 # # Define a function called `draw_line(img,x,y,length,vertical)` that draws a vertical line if the argument vertical is true and horizontal line if false. Use the functions you create in **Question 4** and **Question 5**. # In[ ]: image = SimpleImage.blank(size, size) def draw_line(img,x,y,length,vertical): # YOUR CODE GOES HERE pass # ### Question 7 # # Define a function called `draw_diagonal(img)` that draws a diagonal line from the `pixel(0,0)` to `(size-1,size-1)`. It should look something like this: # # ![](diagonal.png) # In[ ]: image = SimpleImage.blank(size, size) # YOUR CODE GOES HERE # ### Question 8 # # Define a function that draws a triangle. The triangle could be a right angle triangle, an equilateral one as well. # In[ ]: image = SimpleImage.blank(size, size) # YOUR CODE GOES HERE # ### Question 9 # # Define a function that draws a checker board pattern. `Pixel(0,0)` should be white, `Pixel(0,1)` should black be and so on. It should look something like this: # # ![](checkers.png) # In[ ]: image = SimpleImage.blank(size, size) # YOUR CODE GOES HERE # ### Question 10 # # # Write a function that inverts the color of each square in the board above. i.e `Pixel(0,0)` should now be black, `Pixel(0,1)` white and so on. The function should only take the image as an argument and invert the color of each cell in the given image. It should look like this: # # ![](checkers2.png) # In[ ]: # DON'T USE 'image = SimpleImage.blank(size, size)' TO REMOVE THE PREVIOUSLY DRAWN IMAGE. # ### Question 11 # # Now that you have inverted the colors of the board, let's create a function that changes the black tiles to red tiles. It should look something like this: # ![](red_checkers.png) # In[ ]: # DON'T USE 'image = SimpleImage.blank(size, size)' TO REMOVE THE PREVIOUSLY DRAWN IMAGE. # ## Gradients # ### Question 12 # # Make a horizontal gradient from black to white. # # Note: # - The RGB values for black are `[0, 0, 0]` # - The RGB values for white are `[255, 255, 255]` # - The RGB values for grey are `[k, k, k]` for some `k`. Larger `k` is lighter grey, smaller `k` is darker grey. # - as `x` goes from 0 to `size`, `k` should go from 0 to `255`. So compute `k = x*255//size` # # ![](grayscale-horizontal.png) # In[ ]: image = SimpleImage.blank(size, size) # YOUR CODE GOES HERE # ### Question 13 # Change your code so that the gradient goes from black to red. # # Note: # - red is [255, 0, 0] # # ![](red-horizontal.png) # In[ ]: image = SimpleImage.blank(size, size) # YOUR CODE GOES HERE # Change your code so that the gradient is vertical. # # Hint: before, the value `k` depended on `x`. What should it depend on now? # # ![](red-vertical.png) # In[ ]: image = SimpleImage.blank(size, size) # YOUR CODE GOES HERE