Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Image Processing with Python

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?

The Python Imaging Library, (PIL) is the library


for image manipulation, however...
Why Pillow?

… PIL’s last release was in 2009


Why Pillow?

● easier to install
● supports Python 3
● active development
● actually works*

* Non-Pillow PIL really is


frustrating
http://python-imaging.github.io/
What can Pillow do for me?

● Automatically generate thumbnails


Modules:
● Apply image filters (auto-enhance)
● Apply watermarks (alpha layers)
● ImageOps
● Extract images from animated gifs
● ImageMath
● Extract image metadata
● ImageFilter
● Draw text for annotations (and shapes)
● ImageEnhance
● Basically script things that you might do
● ImageStat
in Photoshop or GIMP for large numbers
of images, in Python
Pillow Setup

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

After installing prereqs:


$ pip install Pillow
Documentation

Pillow documentation:
http://pillow.readthedocs.org/en/latest/about.html

Image Basics: http://pillow.readthedocs.org/en/latest/handbook/concepts.


html

List of Modules: http://pillow.readthedocs.org/en/latest/reference/index.


html
Read in an image!

from PIL import Image

im = Image.open(infile)
im.show()

print(infile, im.format, "%dx%d" %


im.size, im.mode)

Important note: Opening an image file is a fast operation, independent of file


size and compression. Pillow will read the file header and doesn’t decode or
load raster data unless it has to.
Basic Methods

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)

● radius – size of the area


● percent – % contrast change allowed in area
● threshold – minimum brightness change needed before filter takes effect
Unsharp masks basically apply a Gaussian blur to a copy of the original image and compare it to the
original. If the difference is greater than a threshold setting, the images are basically subtracted.

Kernel(size, kernel, scale=None, offset=0)

● size – Kernel size, given as (width, height)


● kernel – a sequence containing kernel weights
● scale – the result for each pixel is divided by this value. Default = sum of the kernel weights
● offset – this value is added to the result, after it has been divided by the scale factor
Practical Things: Apply watermarks
from PIL import Image

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

# Important thing is the 3rd argument of the paste


function. Specifies your PNG as alpha layer so that you
avoid a black background.
What is OpenCV?

● Open source computer vision library in C++


● Includes a machine learning library to support computer
vision applications
● OpenCV-Python is the Python API of OpenCV
● Large user base = good documentation and excellent
online tutorials and help
● Huge library, super powerful
OpenCV Fun Facts
● In 2005, OpenCV was used on Stanley, the vehicle who won 2005
DARPA Grand Challenge [1]
● You can solve sudoku puzzles with OpenCV [2]
● OpenCV is under a BSD license [3]
● OpenCV is being used to program new robots like the PR2 [4]
● Shoot squirrels [5]

[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

$ sudo apt-get install python-opencv

Documentation: http://docs.opencv.
org/modules/core/doc/intro.html
Weird things

● Color image loaded by OpenCV is in BGR mode


● But Matplotlib displays in RGB mode
● So color images will not be displayed correctly in
Matplotlib if image is read with OpenCV
Fourier Transforms on 2D images
Use Numpy or Opencv

Center of the image represents


the lower frequencies
More examples of 2D Fourier Transforms

Images from: http://cns-alumni.bu.edu/~slehar/fourier/fourier.html


Mask and Inverse FFT
High Pass Filter Using FFT

Hey look it’s edge-detection!


Questions?

Post questions to the DesertPy G+ page!

Sarah Braden Twitter: @ifmoonwascookie


Alternatives to Pillow?
● PythonMagickWand is an object-oriented Python interface to MagickWand based on ctypes. 21
Jan 2009?
● PythonMagick is an object-oriented Python interface to ImageMagick. Last build 22 January
2014.
● Wand is a ctypes-based ImagedMagick binding library for Python. Last release 17 June 2013.
Pillow Setup
Some (most?) of Pillow's features require external libraries.
● libjpeg provides JPEG functionality.
○ Pillow has been tested with libjpeg versions 6b, 8, and 9
● zlib provides access to compressed PNGs
● libtiff provides group4 tiff functionality
○ Pillow has been tested with libtiff versions 3.x and 4.0
● libfreetype provides type related services
● littlecms provides color management
● libwebp provides the Webp format.
○ Pillow has been tested with version 0.1.3, which does not read transparent webp files.
Version 0.3.0 supports transparency.
Also: you may want to: sudo apt-get install imagemagick
Prerequisites are installed with on Ubuntu 12.04 LTS with
$ sudo apt-get install libtiff4-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms1-dev libwebp-dev

You might also like