In [ ]:
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
In [ ]:
from PIL import Image
In [ ]:
img = Image.open("img/colonies.jpg")
plt.imshow(img)
In [ ]:
arr = np.array(img)
print("x,y,RGB ->",arr.shape)
By convention, channels are arranged: Red, Green, Blue, Transparency
In [ ]:
arr = np.array(img)
plt.imshow(arr)
plt.show()
arr[:,:,1] = 255
plt.imshow(arr)
In [ ]:
x = Image.fromarray(arr)
x.save('junk.png')
In [ ]:
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)
In [ ]:
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))
In [ ]:
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)
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.
In [ ]:
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")
In [ ]: