Wednesday, September 9, 2015

Manipulating Images with the Python Imaging Library

In my previous article on time-saving tips for Pythonists, I mentioned that Python is a language that can inspire love in its users.

One reason for this is the number of time-saving libraries available for this language. A nice example is the Python Imaging Library (PIL), which is the focus of this article.

What You Can Do with PIL

PIL is a free library that adds image processing capabilities to your Python interpreter, supporting a range of image file formats such as PPM, PNG, JPEG, GIF, TIFF and BMP.

PIL offers several standard procedures for image processing/manipulation, such as:

  • pixel-based manipulations
  • masking and transparency handling
  • filtering (for example, blurring, contouring, smoothing, edge detection)
  • image enhancement (for example, sharpening, brightness adjustment, contrast)
  • geometrical, color and other transforms
  • adding text to images
  • cutting, pasting and merging images
  • creating thumbnails.

PIL and Pillow

One issue with PIL is that its most recent version, 1.1.7, was released in 2009, and only supports Python 1.5.2–2.7. Although the PIL site promises a forthcoming version for Python 3.X, its last commit was in 2011, and it appears that development has been discontinued.

Fortunately, all is not lost for Python 3.X users. A project called Pillow has forked the PIL repository and added Python 3.X support. Given that most readers will probably be working with Python 3.X, I'll focus on the Pillow update in this article.

Installing Pillow

Since Pillow supports versions of Python back to Python 2.4, I'll only focus on installing Pillow and not the older version of PIL.

Python on a Mac

I'm currently writing this tutorial on a Mac OS X Yosemite 10.10.5, and thus will describe how to install Pillow on a Mac OS X machine. But, don't worry, I'll provide a link at the end of this section that describes how to install Pillow on other operating systems.

I just want to note here that Mac OS X comes with Python pre-installed. However, the version most likely will be prior to 3.X.

For instance, on my machine, when I run $ python --version in the terminal, I get Python 2.7.10.

Python and pip

A very easy way to install Pillow is through pip.

If you don't have pip installed on your machine, simply type the following command in your terminal, and you're all done:

$ sudo easy_install pip

Now, to install Pillow, simply type the following in your terminal:

$ sudo pip install pillow

That was easy, wasn't it?

As I promised, for installing Pillow on other operating systems, you can find the instructions for that here.

Continue reading %Manipulating Images with the Python Imaging Library%


by A. Hasan via SitePoint

No comments:

Post a Comment