python
%pylab inline
from PIL import Image,ImageDraw,ImageFont
img = Image.open('work-image/lena.jpg')
pl_img = np.array(img) ; plt.imshow( pl_img ) #display

python
#Convert to grayscale
gray_img = img.convert("L")
#Not the intended display
pl_img = np.array(gray_img) ; plt.imshow( pl_img ) #display
#File information display
print('size    : ', gray_img.size)
print('format  : ', gray_img.format) 
print('mode    : ', gray_img.mode) 
print('palette : ', gray_img.palette) 
print('info    : ', gray_img.info) 
#I think I have to do this
import matplotlib.cm as cm
plt.imshow(pl_img, cmap = cm.Greys_r)

Well, this time it's filtering
Start with the built-in filter
python
from PIL import ImageFilter, ImageOps
python
#Blur
pl_img = np.array(gray_img.filter(ImageFilter.BLUR)); plt.imshow(pl_img, cmap = cm.Greys_r)

python
#Contour extraction
pl_img = np.array(gray_img.filter(ImageFilter.CONTOUR)); plt.imshow(pl_img, cmap = cm.Greys_r)

python
#Embossing
pl_img = np.array(filter_img = gray_img.filter(ImageFilter.EMBOSS)); plt.imshow(pl_img, cmap = cm.Greys_r)

python
#Minimum filter
pl_img = np.array(gray_img.filter(ImageFilter.MinFilter(5))); plt.imshow(pl_img, cmap = cm.Greys_r)

I will write the theory later (Is it really written?)
python
#Detects vertical edges
flist =  [1,   1,  1,
          0,   0,  0,
         -1,  -1, -1]
flt = ImageFilter.Kernel((3, 3), flist, scale=1)
filter_img = gray_img.filter(flt)
pl_img = np.array(filter_img) ; plt.imshow(pl_img, cmap = cm.Greys_r)

python
#Detects horizontal edges
flist =  [1,   0, -1,
          1,   0, -1,
          1,   0, -1]
flt = ImageFilter.Kernel((3, 3), flist, scale=1)
filter_img = gray_img.filter(flt)
pl_img = np.array(filter_img) ; plt.imshow(pl_img, cmap = cm.Greys_r)

python
#4 Near proximity Laplacian
flist = [0,  1, 0,
         1, -4, 1,
         0,  1, 0]
flt = ImageFilter.Kernel((3, 3), flist, scale=1)
filter_img = gray_img.filter(flt)
pl_img = np.array(filter_img) ; plt.imshow(pl_img, cmap = cm.Greys_r)

python
#8 Near proximity Laplacian
flist = [1,  1, 1,
         1, -8, 1,
         1,  1, 1]
flt = ImageFilter.Kernel((3, 3), flist, scale=1)
filter_img = gray_img.filter(flt)
pl_img = np.array(filter_img) ; plt.imshow(pl_img, cmap = cm.Greys_r)
filter_img.save('work-image/filter05-lena.png') 

python
#8 Example of using the nearby Laplacian
flist = [0.4,  0.4, 0.4,
         0.4, -2.2, 0.4,
         0.4,  0.4, 0.4]
flt = ImageFilter.Kernel((3, 3), flist, scale=1)
filter_img = gray_img.filter(flt)
pl_img = np.array(filter_img) ; plt.imshow(pl_img, cmap = cm.Greys_r)

↓ I gave a notebook to nbviewer (this is the main one) nbviewer.ipython.org/github/suto3/git-public/blob/master/python/notebook/Pillow-workflow03.ipynb
↓ Click here for the working environment Pillow environment construction --Virtual environment by virtualenv, interactive environment by iPython --Qiita
Try using Pillow on iPython (Part 1) --Qiita
Try using Pillow on iPython (Part 2) --Qiita
Recommended Posts