Try using Pillow on iPython (Part 3)

Pillow work flow (3)

  1. Load the image with Image.open
  2. Convert image to grayscale
  3. Gonyo Gonyo the image
  4. Save the image edited with Image.save

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

lena.jpg

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)

gray_lena.jpg

Well, this time it's filtering

Built-in filter

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)

blur-lena.png

python


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

contour-lena.png

python


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

emboss-lena.png

python


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

minf-lena.png

Own filter

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)

filter01-lena.png

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)

filter02-lena.png

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)

filter03-lena.png

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') 

filter04-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)

filter05-lena.png

↓ 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

Try using Pillow on iPython (Part 1)
Try using Pillow on iPython (Part 2)
Try using Pillow on iPython (Part 3)
Try using OpenCV on Windows
Try using SQLAlchemy + MySQL (Part 1)
Try using SQLAlchemy + MySQL (Part 2)
Try using ArUco on Raspberry Pi
Try using Bash on Windows 10 2 (TensorFlow installation)
Try using Tkinter
Try using docker-py
Try using cookiecutter
Try using geopandas
Try using Selenium
Try using scipy
Try using pandas.DataFrame
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try using PyODE
Graph analysis and visualization on IPython Notebook using Cytoscape / cyREST and py2cytoscape Part 1
Try using a QR code on a Raspberry Pi
Python: Try using the UI on Pythonista 3 on iPad
Try using the Python web framework Tornado Part 1
Make predictions using regression on actual data ~ part1
Try using the Python web framework Tornado Part 2
Visualize network data using Cytoscape from IPython Notebook Part 1
Try using virtualenv (virtualenvwrapper)
Try using E-Cell 4 on Windows 7 or Mac OS X
Try using the temperature sensor (LM75B) on the Raspberry Pi.
I tried using Ipython
Try using virtualenv now
Try using Django templates.html
[Kaggle] Try using LGBM
Try using Python's feedparser.
Try using Python's Tkinter
Try FEniCS on Windows!
Try Poerty on Windows
Try NeosVR on Linux
Notes on using Alembic
Try using Tweepy [Python2.7]
Try deepdream on Mac
Try using Pytorch's collate_fn
Try using PythonTex with Texpad.
Try normal Linux programming Part 7
[Python] Try using Tkinter's canvas
OpenPose on MacBook Pro Part 2
Try using Jupyter's Docker image
Try using scikit-learn (1) --K-means clustering
Make iPython available on OSGeo4W
Try using matplotlib with PyCharm
Try using Azure Logic Apps
Try using Kubernetes Client -Python-
Try using the Twitter API
Try normal Linux programming Part 2
[Django] Notes on using django-debug-toolbar
Displaying strings on IPython Notebook
Try using Jupyter Notebook dynamically
Try normal Linux programming Part 3
Try "100 knocks on data science" ①
Try using AWS SageMaker Studio
Try translating English PDF Part 1