Skip to Content
Programming Computer Vision with Python
book

Programming Computer Vision with Python

by Jan Erik Solem
June 2012
Beginner to intermediate
260 pages
6h 28m
English
O'Reilly Media, Inc.
Content preview from Programming Computer Vision with Python

Chapter 1. Basic Image Handling and Processing

This chapter is an introduction to handling and processing images. With extensive examples, it explains the central Python packages you will need for working with images. This chapter introduces the basic tools for reading images, converting and scaling images, computing derivatives, plotting or saving results, and so on. We will use these throughout the remainder of the book.

1.1 PIL—The Python Imaging Library

The Python Imaging Library (PIL) provides general image handling and lots of useful basic image operations like resizing, cropping, rotating, color conversion and much more. PIL is free and available from http://www.pythonware.com/products/pil/.

With PIL, you can read images from most formats and write to the most common ones. The most important module is the Image module. To read an image, use:

from PIL import Image

pil_im = Image.open('empire.jpg')

The return value, pil_im, is a PIL image object.

Color conversions are done using the convert() method. To read an image and convert it to grayscale, just add convert('L') like this:

pil_im = Image.open('empire.jpg').convert('L')

Here are some examples taken from the PIL documentation, available at http://www.pythonware.com/library/pil/handbook/index.htm. Output from the examples is shown in Figure 1-1.

Convert Images to Another Format

Using the save() method, PIL can save images in most image file formats. Here’s an example that takes all image files in a list of filenames (filelist) and ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning OpenCV 4 Computer Vision with Python 3 - Third Edition

Learning OpenCV 4 Computer Vision with Python 3 - Third Edition

Joseph Howse, Joe Minichino

Publisher Resources

ISBN: 9781449341916Errata PageSupplemental Content