Chapter 15 Exercises

Below is a selection of images that you can use in the programs in this section.

beach.jpgbaby.jpgvangogh.jpgswan.jpg
puppy.jpgkitten.jpggirl.jpgmotorcycle.jpg
gal1.jpgguy1.jpggal2.jpg

Note

Remember that it can take a bit of time to process all the pixels in a picture! Check for errors below the code if it is taking a long time, but if you don’t see any errors just wait.

  1. Make changes to 10 lines in the code below so that it runs. It changes areas that look red in the original to green.

    Add image after the from in line 1. Add a : at the end of line 8. Indent lines 14 - 21 four spaces to the right.

    Show Comments
  2. Fix the code below so that the red in the picture gets changed to blue.

    Fix the indentation like below and make sure the change occurs when the red value is greater than 150 and the green and blue values are less than 100. Fix the colors of the new pixel by setting it to (0,0,b).

    Show Comments
  3. Fix the indention in the code below so that it runs correctly. It does a primitive form of edge detection by getting all of the pixels (except for the last row) and all the pixels to the right of those and determining if the difference between the average of the rgb values for the pixel and the pixel to the right are substantially different.

    Indent lines 9 to 24 as shown below.

    Show Comments
  4. Fix and change the code to change just the background color from white to gray.

    Fix the indentation like below and then make sure the if statement only works for r,g,b values greater than 200. Also, make sure you set the pixel to the new pixel.

    Show Comments
  5. Fix the indention in the code below so that it runs correctly. It posterizes a picture which means that it reduces all the colors in a picture to a small number of colors – like the ones you might use if you were making a poster..

    Indent lines 17, 19, 21, 23, 25, and 27 as shown below.

    Show Comments
  6. Fix the indentation so that the code puts the motorcycle on the beach. The code checks if the pixel isn’t white in the first image, and if it’s not, it places that pixel in the same location on the second image.

    Indent like below.

    Show Comments
  7. Fix 5 errors in the code below. It will copy the non-white pixels from gal1.jpg to guy1.jpg.

    Add a " after the ( on line 5. Add another ) before the : on line 8. Add a : on line 9. Add a y after the , on line 10. Add an and on line 16.

    Show Comments
  8. Fix the 5 errors so that a swan in shown on a beach.

    Add quotations around swan.jpg. The getPixel() should be getPixel(x,y). It should be getRed() instead of getRed. Add a colon after the if statement, and set the new pixel to (x,y,p1).

    Show Comments
  9. Change the code below to use if and else rather than two if statements per color. It posterizes an image.

    Change lines 18, 22, and 26 as shown below.

    Show Comments
  10. Fix the indentation in the code and change it so that it edges the motorcycle but the background is black and the motorcycle edging will be white.

    Fix the indentation like below and set the newPixel to be (255,255,255) if the absolute value of the difference of the averages is greater than 10. Otherwise the pixel should be (0,0,0).

    Show Comments
  11. Change the following code into a procedure. It posterizes an image. Be sure to call it to test it.

    Define the procedure and then import the library and create the image. Pass the image to the posterize procedure.

    Show Comments
  12. Fix the 5 errors in the procedure so that it edges the motorcycle which means the image should only have 2 colors. The motorcycle should be one color, everything else should be the other color.

    The jpg file should be in quotations. You have to change line 3 to for x in range(img.getWidth() - 1):. Add a : after the if statement. Take the 2 lines where you show the changed image outside of the body of both for loops.

    Show Comments
  13. Change the following into a procedure. It changes areas that are mostly red looking to green. Be sure to call it to test it.

    Define the procedure and then import the library and create the image. Pass the image to the changeRedToGreen procedure.

    Show Comments
  14. The code below currently makes the picture gray. Change it so that it posterizes (reduce the number of colors) the image instead.

    Set the values to the new color to 0 if the pixel color is less than 120 and 120 if it is greater than or equal to 120.

    Show Comments
  15. Write the code to posterize a picture but use 3 values for each color instead of 2. Use 0 if the current value is less than 85, use 85 if the value is less than 170, else use 170.

    See the code in lines 16-33 for how to do this.

    Show Comments
  16. Fix the errors in the code and change the code to use if’s and else’s instead of just if’s.

    In line 7, add a ) and : to the end. In line 9, it should be getPixel(x,y) not getPixels(x,y). The change the image line of code should be in the body of the inner for loop. Change all the second if clauses to else clauses.

    Show Comments
  17. Write the code to do edge detection on a picture, but compare the curent pixel with the one below it rather than the one to the right.

    See the code below. Only lines 7 trough 10 needed to change. We now loop through all the columns and all but one of rows. We compare the pixel at the current x and y to the one at the same x but y+1.

    Show Comments
  18. Write a procedure that takes an image as a parameter and edges it using the colors blue and white.

    Define a procedure similar to the one below.

    Show Comments
  19. Write a procedure to remove the red on very red pixels (pixels that have a red value greater than 200 and a green and blue value of less than 100).

    Define the procedure and then import the library and create the image. Pass the image to the removeVeryRed procedure.

    Show Comments
  20. Write a procedure that takes a picture as a parameter and converts all the red to grayscale.

    Show Comments
You have attempted of activities on this page