from PIL import Image as IM
import numpy as np
import random
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
%load_ext autoreload
%autoreload 2
# Load a standard photo
im = IM.open('/Users/YRZ/Downloads/1.jpg')
# Convert it to gray-scale image
im = im.convert("L")
plt.imshow(im, cmap = plt.cm.gray_r)
plt.show()
# Check image size
print(im.size)
# rotate 0 degrees
rot = im.rotate(0)
plt.imshow(rot, cmap = plt.cm.gray_r)
plt.show()
# mask(left, upper, right, lower) CAUTION: Delta's should be ingeters!
mask = (445, 384, 625, 520)
region = im.crop(mask)
plt.imshow(region, cmap = plt.cm.gray_r)
plt.show()
# Detailed check:
# Adjust the mask until detailed sub-images become perfect.
# Num of Rows and Cols
NUM_R, NUM_C = 4, 5
# Delta length of Height and Width
DELTA_H = (mask[3] - mask[1]) / NUM_R
DELTA_W = (mask[2] - mask[0]) / NUM_C
plt.title("first row")
plt.imshow(region.crop((0,0,DELTA_W*NUM_C,DELTA_H)), cmap = plt.cm.gray_r)
plt.show()
plt.title("first col")
plt.imshow(region.crop((0,0,DELTA_W,DELTA_H*NUM_R)), cmap = plt.cm.gray_r)
plt.show()
plt.title("last row")
plt.imshow(region.crop((0,DELTA_H*(NUM_R-1),DELTA_W*NUM_C,DELTA_H*NUM_R)), cmap = plt.cm.gray_r)
plt.show()
plt.title("last col")
plt.imshow(region.crop((DELTA_W*(NUM_C-1),0,DELTA_W*NUM_C,DELTA_H*NUM_R)), cmap = plt.cm.gray_r)
plt.show()
# Filters
from PIL import ImageFilter
def filter(img):
# fimg = img.filter(ImageFilter.EDGE_ENHANCE)
fimg = img.filter(ImageFilter.MedianFilter(3))
# fimg = fimg.filter(ImageFilter.SHARPEN)
return fimg
imfilter = filter(region)
plt.imshow(imfilter, cmap = plt.cm.gray_r)
plt.show()