Python Img Proc PDF
Python Img Proc PDF
Desert Py Meetup
26 February 2014
Sarah E. Braden
Overview
● Pillow
○ Pillow is a fork of PIL, the Python Imaging Library
○ http://python-imaging.github.io/
● OpenCV
○ http://opencv.org/
● Files for this presentation
○ https://github.com/desertpy/presentations
Why Pillow?
● easier to install
● supports Python 3
● active development
● actually works*
Pillow’s prerequisites:
https://pypi.python.org/pypi/Pillow/2.1.0#platform-specific-instructions
Warning!
Since some (most?) of Pillow's features require external libraries, prerequisites
can be a little tricky
Pillow documentation:
http://pillow.readthedocs.org/en/latest/about.html
im = Image.open(infile)
im.show()
geometric transforms:
out = im.resize((128, 128))
out = im.rotate(45) # degrees counter-clockwise
out = im.transpose(Image.FLIP_LEFT_RIGHT)
out = im.transpose(Image.FLIP_TOP_BOTTOM)
crop:
box = (100, 100, 400, 400) #(left, upper, right, lower)
region = im.crop(box)
Practical Things: Make thumbnails
Practical Things: Apply filters
The ImageFilter module contains a number of pre-defined enhancement filters
that can be used with the filter() method:
● BLUR
● CONTOUR
● DETAIL
from PIL import Image, ImageFilter
● EDGE_ENHANCE
● EDGE_ENHANCE_MORE
● EMBOSS
out1 = im.filter(ImageFilter.BLUR)
● FIND_EDGES out2 = im.filter(ImageFilter.GaussianBlur(radius=20)
● SMOOTH
● SMOOTH_MORE
● SHARPEN
Practical Things: Apply filters
UnsharpMask(radius=2, percent=150, threshold=3)
baseim = Image.open(imgfile)
logoim = Image.open(watermark) #transparent image
baseim.paste(logoim, (baseim.size[0]-logoim.size[0],
baseim.size[1]-logoim.size[1]), logoim)
baseim.save('new.png',"PNG")
[1] https://www.willowgarage.com/pages/software/opencv
[2] http://sudokugrab.blogspot.com/2009/07/how-does-it-all-work.html
[3] opencv.org
[4] http://hackermedley.org/opencv/ - interview w/OpenCV creator Gary Bradski
[5] http://www.youtube.com/watch?v=QPgqfnKG_T4
Install OpenCV for Python
Documentation: http://docs.opencv.
org/modules/core/doc/intro.html
Weird things