Chapter 11 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. Fix 6 syntax errors in the code below so that it correctly sets the red in all pixels to 0.

  2. The code below makes the image have a green-blue tint. Change 1 thing in order to make it have a red tint instead.

    Show Comments
  3. Fix the indention below to correctly set the red to the green, the green to the blue, and the blue to the red.

  4. Fix the 5 errors in the code, so that the Red pixels get the value of the green, the green get the value of blue, and the blue get the value of the red. (The cat should look purple and gray)

  5. Fill in the missing code on lines 9, 12, and 18 below to set the red to half the original value in all pixels in the picture.

  6. Complete the code in order to set the blue value to an eighth of the green value plus an eighth of the red value.

  7. Fix the indention in the code below so that it correctly increases the red in each pixel in the picture by 1.5.

  8. This code is supposed to make the picture completely black; however, it is taking forever when it should only take a few seconds. Fix the code (without adding anything new) so that it runs in a few seconds.

  9. Fix the code below to correctly set the green and blue values to 0.75 times their current values.

  10. Write code below to set all the pixels to half their original values. Use the // operator so that the color values are integers.

  11. The following code redraws the whole image by drawing the top half and then drawing the bottom half. Change it to set the red to 0 for all pixels in the top and the green to 0 in all the pixels in the bottom of the picture.

  12. The code below makes the whole image have a blue-green tint. Change the code so that it makes an only blue tint in the bottom left corner.

  13. Change the code below to set the red value in the pixels in the bottom half of the picture to 0.

  14. The code below makes the whole image seem red. Change it, so that only every 5 pixels get changed, so that it will look like a red grid.

  15. Fill in the missing values in the procedure to keep only the green value of each pixels in an image. Call the proceedure with the baby.jpg image to test your result.

  16. A grayscale picture is when the red, green, and blue value of a pixel are all equal to the average of the original pixel value. Write the code to turn the left half of an image into gray scale.

  17. Define a procedure to negate an image. See Image_Negate_Quarter from Chapter 11 section 7 for how to create a negative of an image. Pass the image to the procedure. Do the import, create the image, call the prodecure, and show the result.

  18. Write code that takes the top half of an image and replicates it in the bottom half.

  19. Write a procedure to mirror an image from left to right around a vertical line in the middle of the image. Pass the image to the procedure. Do the import, create the image, call the prodecure, and show the result.

  20. Write code that flips the image across a horizontal line.

Next Section - Chapter 12 - A Computer can Make Decisions