Chapter 11 Exercises¶
Below is a selection of images that you can use in the programs in this section.
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.
Fix 4 syntax errors in the code below so that it correctly sets the red in all pixels to 0.
*at the end of line 1. Add a
)at the end of line 4. Change line 11 to
(0). Change line 14 to
The code below makes the image have a green-blue tint. Change 1 thing in order to make it have a red tint instead.
Set the Red pixel to be 255.
Fix the indention below to correctly set the red to the green, the green to the blue, and the blue to the red.
Change the indention on lines 11-22 as shown below.
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)
The image passed should have the
.jpgat the end. Make sure the
r,g,bvariables are correctly set to represent red, green, and blue. In the for loop header, make sure it’s looping through pixels not pixel, and it should be
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.
in pixels:to line 9. Add
getRed()to line 12. Add
updatePixel(p)to line 18.
Complete the code in order to set the blue value to an eigth of the green value plus an eight of the red value.
Complete the for loop header to iterate through pixels. Get the values for the blue, red, and green pixels. Set the blue pixels to the sum of an eighth of both the red and green pixels.
Fix the indention in the code below so that it correctly increases the red in each pixel in the picture by 1.5.
Remove the indention on lines 4-5. Add indention on lines 14-15.
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.
The last two lines should be outside the for loop.
Fix the code below to correctly set the green and blue values to 0.75 times their current values.
Get the values into
bbefore you try to use them. Multiply the old values by
The code below sets all the pixels to half their original values with one for loop. Change the code so it uses 2 for loops that utilize the range function (1 for loop should be nested in the other).
Change the for loop to iterate through the range of the width and then the nested loop should iterate through the range of the height. Call the get pixel method inside the for loop as shown below.
Change the following code to set the red to 0 for all pixels in the left half of the picture.
Change line 7 to
int(img.getWidth() / 2)):.
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.
You have to iterate through the first half of the image width and the second half of the image height. Also, in the body of the loop, set the green pixel to 0 too.
Change the code below to set the red value in the pixels in the bottom half of the picture to 0.
Change line 8 to
(int(img.getHeight() / 2), img.getHeight()).
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.
In the for loops, add an argument of 0 before the existing argument and add a 5 after the existing argument in the range method.
Change the following code into a procedure to keep only the green values in all pixels in a picture.
Define a procedure to keep only the green values (set the red and blue to 0) in an image. Pass the image to the procedure. Do the import, create the image, call the prodecure, and show the result.
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.
Iterate through only half of the width. Set each color to the average value of the original pixel values.
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.
Define the procedure as shown below.
Write code that takes the top half of an image and replicates it in the bottom half.
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.
Create a procedure as shown below. Call it to test it.
Write code that flips the image across a horizontal line.
Follow the code below.