CS 100

Logo

Scientific Computing

Activity 29 - Pictures 🎨

Complete the setup to install the cImage module and read some key differences based on the latest updates to the module.

Create a file called line.py and save it to your cs100/ch6 folder.

  1. Copy the following code into your file:
    from image import *
       
    myWin = ImageWin(300, 300, "Class image")
       
    # create a new image which we modify
    img = EmptyImage(300,300)
       
    blackPixel = Pixel(0,0,0)
       
    # diagonal pixels are colored black
    for i in range(300):
        img.setPixel(i,i,blackPixel)
           
    # YOUR CODE HERE (change image before it is displayed & saved)
       
    # displays the image
    img.draw(myWin)
       
    # saves image to the given file name
    img.save("classImg.gif")
       
    # when you click anywhere on the window, it closes
    myWin.exitOnClick()
    
  2. Run the program. Inspect the code to understand what each line does.

  3. Create a red square in the upper left corner which has width 100 pixels and height 100 pixels.

  4. Add a rectangle in the upper right corner which has width 200 pixels and height 100 pixels, where each pixel is a random color.

If you finish early

  1. Add more shapes to your new image. What else can you create? A triangle? Maybe a tree with leaves on it? A house? A smiley face?

How to submit

Submit your working line.py file to Moodle.