Skip to main content

Section 18.10 Write Code Questions

Note 18.10.1.

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.
Data: gal2.jpg
Figure 18.10.2. Figure: A picture of a young woman in a red t-shirt.

Activity 18.10.3.

Fix 4 syntax errors in the code below so that it correctly sets the red in all pixels to 0.
Solution.
Fix 4 syntax errors in the code below so that it correctly sets the red in all pixels to 0.
Data: kitten.jpg
Figure 18.10.4. Figure: "A picture of a black and white striped kitten with some green plants behind it.

Activity 18.10.5.

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

Activity 18.10.6.

Fix the indention below to correctly set the red to the green, the green to the blue, and the blue to the red.
Solution.
Fix the indention below to correctly set the red to the green, the green to the blue, and the blue to the red.

Activity 18.10.7.

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)
Solution.
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)

Activity 18.10.8.

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.
Solution.
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.
Data: swan.jpg
Figure 18.10.9. Figure: A picture of a white swan in a pond.

Activity 18.10.10.

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

Activity 18.10.11.

Fix the indention in the code below so that it correctly increases the red in each pixel in the picture by 1.5.
Solution.
Fix the indention in the code below so that it correctly increases the red in each pixel in the picture by 1.5.
Data: motorcycle.jpg
Figure 18.10.12. Figure: A picture of an orange and gray motorcycle on a white background.

Activity 18.10.13.

Fix the code (without adding anything new) to make the picture completely black.
Solution.
Fix the code (without adding anything) to make the picture completely black.

Activity 18.10.14.

Fix the code below to correctly set the green and blue values to 0.75 times their current values.
Solution.
Fix the code below to correctly set the green and blue values to 0.75 times their current values.
Data: baby.jpg
Figure 18.10.15. Figure: A picture of a toddler standing.

Activity 18.10.16.

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).
Solution.
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).

Activity 18.10.17.

Change the following code to set the red to 0 for all pixels in the left half of the picture.
Solution.
Change the following code to set the red to 0 for all pixels in the left half of the picture.

Activity 18.10.18.

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.
Solution.
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.

Activity 18.10.19.

Change the code below to set the red value in the pixels in the bottom half of the picture to 0.
Solution.
Change the code below to set the red value in the pixels in the bottom half of the picture to 0.
Data: guy1.jpg
Figure 18.10.20. Figure: A picture of a young man in a yellow shirt.

Activity 18.10.21.

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.
Solution.
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.

Activity 18.10.22.

Change the following code into a procedure to keep only the green values in all pixels in a picture.
Solution.
Change the following code into a procedure to keep only the green values in all pixels in a picture.

Activity 18.10.23.

Loading a select question (grading type: first)...

Activity 18.10.24.

Loading a select question (grading type: first)...

Activity 18.10.25.

Loading a select question (grading type: first)...

Activity 18.10.26.

Loading a select question (grading type: first)...

Data: vangogh.jpg
Figure 18.10.27. Figure: A picture of a man and woman dancing cheek to cheek from Vincent van Gogh

Activity 18.10.28.

Loading a select question (grading type: first)...

Activity 18.10.29.

Write code to create a subclass of the Image class with three different image effect functions and use each effect on a different part of the image. For example, you could turn the image into grayscale, keep only the green values, swap the red and green values, negate the image, etc. This is one possible solution. For example, the image shown below has one image effect on the first third of the image (negate), one on the second third (clear blue), and one on the final third (reduce red). It is using the image gal2.jpg
Solution.
Create a subclass of the Image class with three different image effect functions and use each effect on a different part of the image. For example, you could turn the image into grayscale, keep only the green values, swap the red and green values, negate the image, etc. This is one possible solution. For example, the image shown below has one image effect on the first third of the image (negate), one on the second third (clear blue), and one on the final third (reduce red). It is using the image gal2.jpg
You have attempted of activities on this page.