<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Manipulating Images in Python
Step1:  The Python Image Library (PIL)
Step2:  You can load images as Image instances
Step3:  Image instances can be interconverted with numpy arrays
Step4:  Values are represented for different channels in bits
8-bit channels have $2^8 = 256$ possible values. 
16-bit channels have $2^{16} = 65,6536$ possible values 
Major formats you'll encounter
8-bit grayscale (1 channel, 0-255). 
8-bit color (3 channels, each 0-255)
8-bit RGBA (4 channels, each 0-255)
16-bit grayscale (1 channel, 0-65535)
16-bit color (3 channels, each 0-65535)
16-bit RGBA (4 channels, each 0-65535)
By convention, channels are arranged
Step5:  Can convert arrays back to images and save
Step6:  One powerful method is to create a mask
Step7:  You can combine masks with "*" (and) and "+" (or)
Predict the output of the next cell.
Step8:  Combined mask
What will the following code do?
Step9:  Find pixels with some green but little red or blue, then set those to green.  All other pixels are black.
Write a block of code that reads in the image, sets any pixel with red > 20 to (0,0,0), and then writes out a new .png image.
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
Explanation: Manipulating Images in Python
End of explanation
from PIL import Image
Explanation: The Python Image Library (PIL)
End of explanation
img = Image.open("img/colonies.jpg")
plt.imshow(img)
Explanation: You can load images as Image instances
End of explanation
arr = np.array(img)
print("x,y,RGB ->",arr.shape)
Explanation: Image instances can be interconverted with numpy arrays
End of explanation
arr = np.array(img)
plt.imshow(arr)
plt.show()
arr[:,:,1] = 255
plt.imshow(arr)
Explanation: Values are represented for different channels in bits
8-bit channels have $2^8 = 256$ possible values. 
16-bit channels have $2^{16} = 65,6536$ possible values 
Major formats you'll encounter
8-bit grayscale (1 channel, 0-255). 
8-bit color (3 channels, each 0-255)
8-bit RGBA (4 channels, each 0-255)
16-bit grayscale (1 channel, 0-65535)
16-bit color (3 channels, each 0-65535)
16-bit RGBA (4 channels, each 0-65535)
By convention, channels are arranged:
Red, Green, Blue, Transparency
What kind of image did we just read in?
What does the following code do?
End of explanation
x = Image.fromarray(arr)
x.save('junk.png')
Explanation: Can convert arrays back to images and save
End of explanation
arr = np.array(img)
low_red = arr[:,:,0] < 50 
arr[:,:,0] = low_red*255
arr[:,:,1] = low_red*255
arr[:,:,2] = low_red*255
plt.show()
plt.imshow(arr)
Explanation: One powerful method is to create a mask
End of explanation
print("True and True =", bool(True*True))
print("False and True =", bool(False*True))
print("Flase and False =",bool(False*False))
print("True or False =",bool(True + False))
print("True or True =",bool(True + True))
print("False or False =",bool(False + False))
Explanation: You can combine masks with "*" (and) and "+" (or)
Predict the output of the next cell.
End of explanation
arr = np.array(img)
red = arr[:,:,0] < 2
green = arr[:,:,1] > 5
blue = arr[:,:,2] < 5
mask = red*green*blue
new_arr = np.zeros(arr.shape,dtype=arr.dtype)
new_arr[:,:,1] = mask*255
plt.imshow(img)
Explanation: Combined mask
What will the following code do?
End of explanation
img = Image.open("img/colonies.jpg")
arr = np.array(img)
mask = arr[:,:,0] > 20
arr[mask,0] = 0
arr[mask,1] = 0
arr[mask,2] = 0
plt.imshow(arr)
img2 = Image.fromarray(arr)
img2.save("junk.png")
Explanation: Find pixels with some green but little red or blue, then set those to green.  All other pixels are black.
Write a block of code that reads in the image, sets any pixel with red > 20 to (0,0,0), and then writes out a new .png image.
End of explanation
<END_TASK>