Python Image Manipulation

0
159
4 min read

(For more resources on Python, see here.)

So let’s get on with it!

Installation prerequisites

Before we jump in to the main topic, it is necessary to install the following packages.

Python

In this article, we will use Python Version 2.6, or to be more specific, Version 2.6.4. It can be downloaded from the following location: http://python.org/download/releases/

Windows platform

For Windows, just download and install the platform-specific binary distribution of Python 2.6.4.

Other platforms

For other platforms, such as Linux, Python is probably already installed on your machine. If the installed version is not 2.6, build and install it from the source distribution. If you are using a package manager on a Linux system, search for Python 2.6. It is likely that you will find the Python distribution there. Then, for instance, Ubuntu users can install Python from the command prompt as:

$sudo apt-get python2.6

Note that for this, you must have administrative permission on the machine on which you are installing Python.

Python Imaging Library (PIL)

We will learn image-processing techniques by making extensive use of the Python Imaging Library (PIL) throughout this article. PIL is an open source library. You can download it from http://www.pythonware.com/products/pil/. Install the PIL Version 1.1.6 or later.

Windows platform

For Windows users, installation is straightforward—use the binary distribution PIL 1.1.6 for Python 2.6.

Other platforms

For other platforms, install PIL 1.1.6 from the source. Carefully review the README file in the source distribution for the platform-specific instructions. Libraries listed in the following table are required to be installed before installing PIL from the source. For some platforms like Linux, the libraries provided in the OS should work fine. However, if those do not work, install a pre-built “libraryName-devel” version of the library. For example, for JPEG support, the name will contain “jpeg-devel-“, and something similar for the others. This is generally applicable to rpm-based distributions. For Linux flavors like Ubuntu, you can use the following command in a shell window.

$sudo apt-get install python-imaging

However, you should make sure that this installs Version 1.1.6 or later. Check PIL documentation for further platform-specific instructions. For Mac OSX, see if you can use fink to install these libraries. See http://www.finkproject.org/ for more details. You can also check the website http://pythonmac.org or Darwin ports website http://darwinports.com/ to see if a binary package installer is available. If such a pre-built version is not available for any library, install it from the source.

The PIL prerequisites for installing PIL from source are listed in the following table:

Library URL Version Installation options
(a) or (b)
libjpeg (JPEG support) http://www.ijg.org/files 7 or 6a or 6b (a) Pre-built version. For example:
jpeg-devel-7
Check if you can do:
sudo apt-install libjpeg (works on some flavors of Linux)
(b) Source tarball. For example: jpegsrc.v7.tar.gz

zib

(PNG support)

http://www.gzip.org/zlib/ 1.2.3 or later (a) Pre-built version. For example:
zlib-devel-1.2.3..
(b) Install from the source.
freetype2
(OpenType /TrueType support)
http://www.freetype.org 2.1.3 or later (a) Pre-built version. For example:
freetype2-devel-2.1.3..
(b) Install from the source.

PyQt4

This package provides Python bindings for Qt libraries. We will use PyQt4 to generate GUI for the image-processing application that we will develop later in this article. The GPL version is available at: http://www.riverbankcomputing.co.uk/software/pyqt/download.

Windows platform

Download and install the binary distribution pertaining to Python 2.6. For example, the executable file’s name could be ‘PyQt-Py2.6-gpl-4.6.2-2.exe’. Other than Python, it includes everything needed for GUI development using PyQt.

Other platforms

Before building PyQt, you must install SIP Python binding generator. For further details, refer to the SIP homepage: http://www.riverbankcomputing.com/software/sip/.

After installing SIP, download and install PyQt 4.6.2 or later, from the source tarball. For Linux/Unix source, the filename will start with PyQt-x11-gpl-.. and for Mac OS X, PyQt-mac-gpl-... Linux users should also check if PyQt4 distribution is already available through the package manager.

Summary of installation prerequisites

 

Package Download location Version Windows platform Linux/Unix/OS X platforms
Python http://python.org/download/releases/ 2.6.4 (or any 2.6.x) Install using binary distribution (a) Install from binary; Also install additional developer packages (For example, with python-devel in the package name in the rpm systems) OR
(b) Build and install from the source tarball.
(c) MAC users can also check websites such as http://darwinports.com/ or http://pythonmac.org/.
PIL http://www.pythonware.com/products/pil/ 1.1.6 or later Install PIL 1.1.6 (binary) for Python 2.6 (a) Install prerequisites if needed. Refer to Table #1 and the README file in PIL source distribution.
(b) Install PIL from source.
(c) MAC users can also check websites like http://darwinports.com/ or http://pythonmac.org/.
PyQt4 http://www.riverbankcomputing.co.uk/software/pyqt/download 4.6.2 or later Install using binary pertaining to Python 2.6 (a) First install SIP 4.9 or later.
(b) Then install PyQt4.

LEAVE A REPLY

Please enter your comment!
Please enter your name here