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)
Let's create a blank image with a white background, that is an 11 by 11 square.
size = 11
Now, let's set the middle pixel to a black dot by using the set_rgb()
function.
After painting the center pixel of the picture black, let us paint the four corners black.
It should look something like this.
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.
image = SimpleImage.blank(size, size)
def draw_horizontal(img,x,y,length):
# YOUR CODE GOES HERE
pass
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.
image = SimpleImage.blank(size, size)
# YOUR CODE GOES HERE
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.
image = SimpleImage.blank(size, size)
def draw_line(img,x,y,length,vertical):
# YOUR CODE GOES HERE
pass
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:
image = SimpleImage.blank(size, size)
# YOUR CODE GOES HERE
Define a function that draws a triangle. The triangle could be a right angle triangle, an equilateral one as well.
image = SimpleImage.blank(size, size)
# YOUR CODE GOES HERE
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:
image = SimpleImage.blank(size, size)
# YOUR CODE GOES HERE
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:
# DON'T USE 'image = SimpleImage.blank(size, size)' TO REMOVE THE PREVIOUSLY DRAWN IMAGE.
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:
# DON'T USE 'image = SimpleImage.blank(size, size)' TO REMOVE THE PREVIOUSLY DRAWN IMAGE.
Make a horizontal gradient from black to white.
Note:
[0, 0, 0]
[255, 255, 255]
[k, k, k]
for some k
. Larger k
is lighter grey, smaller k
is darker grey.x
goes from 0 to size
, k
should go from 0 to 255
. So compute k = x*255//size
image = SimpleImage.blank(size, size)
# YOUR CODE GOES HERE
Change your code so that the gradient goes from black to red.
Note:
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?
image = SimpleImage.blank(size, size)
# YOUR CODE GOES HERE