Getting ready

The following script makes use of Python Image Library (PIL); a compatible version for Python 3 is Pillow.

Pillow has not been included in the Raspbian repository (used by apt-get); therefore, we will need to install Pillow using a Python Package Manager called PIP.

To install packages for Python 3, we will use the Python 3 version of PIP (this requires 50 MB of available space).

The following commands can be used to install PIP:

sudo apt-get update
sudo apt-get install python3-pip 

Before you use PIP, ensure that you have installed libjpeg-dev to allow Pillow to handle JPEG files. You can do this using the following command:

sudo apt-get install libjpeg-dev
  

Now you can install Pillow using the following PIP command:

sudo pip-3.2 install pillow  

PIP also makes it easy to uninstall packages using uninstall instead of install.

Finally, you can confirm that it has installed successfully by running python3:

>>>import PIL
>>>help(PIL)  

You should not get any errors and see lots of information about PIL and its uses (press Q to finish). Check the version installed as follows:

>>PIL.PILLOW_VERSION

You should see 2.7.0 (or similar).

PIP can also be used with Python 2 by installing pip-2.x using the following command:

sudo apt-get install python-pip

Any packages installed using sudo pip install will be installed just for Python 2.