Chapter 11 Exercises¶
Below is a selection of images that you can use in the programs in this section.
beach.jpg | baby.jpg | vangogh.jpg | swan.jpg |
![]() | ![]() | ![]() | ![]() |
puppy.jpg | kitten.jpg | girl.jpg | motorcycle.jpg |
![]() | ![]() | ![]() | ![]() |
gal1.jpg | guy1.jpg | gal2.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.
Fix 6 syntax errors in the code below so that it correctly sets the red in all pixels to 0.
1# Use the image library
2from PIL import
3
4# Connect to an image file
5img = Image.open('gal2.jpg'
6
7# Load the pixels
8pixels = img.load
9
10# Loop through the pixels
11for x in range(img.size[0]):
12for y in range(img.size[1]):
13r, g, b = pixels[]
14
15# Set the red to 0
16pixels[x, y] = , g, b
17
18# Show the result
19img.show
20
(ch11ex1q)
The code below makes the image have a green-blue tint. Change 1 thing in order to make it have a red tint instead.
201# Use the image library
2from PIL import Image
3
4# Connect to the image
5img = Image.open('puppy.jpg')
6
7# Load the pixels
8pixels = img.load()
9
10# Loop through the pixels
11for x in range(img.size[0]):
12for y in range(img.size[1]):
13r, g, b = pixels[x, y]
14
15# Set the color
16pixels[x, y] = 0, g, b
17
18# Show the result
19img.show()
20
(ch11ex2q)
Fix the indention below to correctly set the red to the green, the green to the blue, and the blue to the red.
221# Step 1: Get the Image object from the PIL library
2from PIL import Image
3
4# Step 2: Connect to the image
5img = Image.open('beach.jpg')
6
7# Step 3: Load the pixels
8pixels = img.load()
9
10# Step 4: Loop through the pixels
11for x in range(img.size[0]):
12for y in range(img.size[1]):
13
14# Step 5: Get the data
15r, g, b = pixels[x, y]
16
17# Step 6: Modify the color
18pixels[x, y] = g, b, r
19
20# Step 7: Show the result
21img.show()
22
(ch11ex3q)
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)
221# Step 1: Load the Image class from the PIL library
2from PIL import Image
3
4# Step 2: Connect to the image
5img = Image.open'kitten.jpg')
6
7# Step 3: Load the pixels
8pixels = imgload()
9
10# Step 4: Loop through the pixels
11for x in range(img.size[]):
12for y in range(img.size[1]):
13
14# Step 5: Get the data
15r, g, b = pixels[x, y[
16
17# Step 6: Modify the color
18pixels[x y] = g, b, r
19
20# Step 7: Show the result
21img.show()
22
(ch11ex4q)
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.
231# Step 1: Load the Image class from the PIL library
2from PIL import Image
3
4# Step 2: Connect to the image
5img = Image.load('beach.jpg')
6
7# Step 3: Load the pixels
8pixels = img.load()
9
10# Step 4: Loop through the pixels
11for x in ??
12for y in ??
13
14# Step 5: Get the data
15?? = ??
16
17# Step 6: Modify the color
18pixels[x, y] = ??, ??, ??
19
20
21# Step 7: Show the result
22img.show()
23
(ch11ex5q)
Complete the code in order to set the blue value to an eighth of the green value plus an eighth of the red value.
141from PIL import Image
2
3img = Image.open('swan.jpg')
4pixels = img.load()
5
6for x in range(img.size[0]):
7for y in range(img.size[1]):
8
9r, g, b = pixels[x, y]
10new_b = ??
11pixel[x, y] = r, g, new_b
12
13img.show()
14
(ch11ex6q)
Fix the indention in the code below so that it correctly increases the red in each pixel in the picture by 1.5.
131from PIL import Image
2
3img = Image.open('beach.jpg')
4pixels = img.load()
5
6for x in range(img.size[0]):
7for y in range(img.size[1]):
8
9r, g, b = pixels[x, y]
10pixels[x, y] = int(1.5 * r), g, b
11
12img.show()
13
(ch11ex7q)
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.
101from PIL import Image
2
3img = Image.open('motorcycle.jpg')
4pixels = img.load()
5
6for x in range(img.size[0]):
7for y in range(img.size[1]):
8pixels[x, y] = 0, 0, 0
9img.show()
10
(ch11ex8q)
Fix the code below to correctly set the green and blue values to 0.75 times their current values.
221# Step 1: Import the Image class from the PIL library
2from PIL import Image
3
4# Step 2: Connect to the image
5img = Image.open('beach.jpg')
6
7# Step 3: Load the pixels
8pixels = img.load()
9
10# Step 4: Loop through the pixels
11for x in range(img.size[0]):
12for y in range(img.size[1]):
13
14# Step 5: Get the data
15r, g, b = pixels[x, y]
16
17# Step 6: Modify the data
18pixels[x, y] = r, int(??), int(??)
19
20# Step 7: Show the result
21img.show()
22
(ch11ex9q)
Write code below to set all the pixels to half their original values. Use the // operator so that the color values are integers.
31
2
3
(ch11ex10q)
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.
151from PIL import Image
2
3img = Image.open('gal2.jpg')
4pixels = img.load()
5
6for x in range(img.size[0]):
7for y in range(img.size[1] // 2):
8r, g, b = pixels[x, y]
9pixels[x, y] = r, g, b
10for y in range(img.size[1] // 2, img.size[1]):
11r, g, b = pixels[x, y]
12pixels[x, y] = r, g, b
13
14img.show()
15
(ch11ex11q)
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.
141from PIL import Image
2
3img = Image.open('vangogh.jpg')
4pixels = img.load()
5width = img.size[0]
6height = img.size[1]
7
8for x in range(width):
9for y in range(height):
10r, g, b = pixels[x, y]
11pixels[x, y] = 0, g, b
12
13img.show()
14
(ch11ex12q)
Change the code below to set the red value in the pixels in the bottom half of the picture to 0.
141from PIL import Image
2
3img = Image.open('gal2.jpg')
4pixels = img.load()
5width = img.size[0]
6height = img.size[1]
7
8for x in range(width):
9for y in range(height):
10r, g, b = pixels[x, y]
11pixels[x, y] = r, g, b
12
13img.show()
14
(ch11ex13q)
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.
141from PIL import Image
2
3img = Image.open('guy1.jpg')
4w = img.size[0]
5h = img.size[1]
6pixels = img.load()
7
8for x in range(w):
9for y in range(h):
10r, g, b = pixels[x, y]
11pixels[x, y] = r, 0, 0
12
13img.show()
14
(ch11ex14q)
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.
181from PIL import Image
2
3def show_only_green(image_file_name):
4img = Image.open(??)
5w = img.??
6h = img.??
7pixels = img.load()
8
9for x in range(w):
10for y in range(h):
11r, g, b = ??
12pixels[x, y] = ??, ??, ??
13
14img.show()
15
16# call proceedure with the baby.jpg
17??
18
(ch11ex15q)
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.
31
2
3
(ch11ex16q)
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.
31
2
3
(ch11ex17q)
Write code that takes the top half of an image and replicates it in the bottom half.
31
2
3
(ch11ex18q)
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.
31
2
3
(ch11ex19q)
Write code that flips the image across a horizontal line.
31
2
3
(ch11ex20q)