How to setup Python Imaging Library, Pillow, on Raspbian Stretch Lite for processing images on your Raspberry Pi
When you are building a Raspberry Pi project that deals with images, the Python Imaging Library, Pillow can be very useful. For example, if you connect a camera to your Raspberry Pi 2 or 3 and took a picture, you may want to resize the picture before sending it to a server endpoint.
In case you have trouble setting up Pillow on Raspbian Stretch Lite, this post is for your reference.
Setting up Raspbian Stretch Lite on your Raspberry Pi for running Python 3 applications
It is recommended that you build your Python application with Python 3.
- How to setup Raspbian Stretch Lite on Raspberry Pi 3 to run Python 3 applications
- How to setup Raspbian Stretch Lite on Raspberry Pi Zero W to run Python 3 applications
For the purpose of this guide, we will be basing this guide from one of the above articles.
Installing dependencies for Pillow on Raspbian Stretch Lite
Pillow has a couple of dependencies that we need to install on our Raspbian Stretch Lite. In order to fulfil such dependences, we will run the following commands in a terminal session:
sudo apt-get update sudo apt-get install libjpeg-dev -y sudo apt-get install zlib1g-dev -y sudo apt-get install libfreetype6-dev -y sudo apt-get install liblcms1-dev -y sudo apt-get install libopenjp2-7 -y sudo apt-get install libtiff5 -y
Preparing the virtual environment to run Python 3 application that uses Pillow
After we had installed the dependencies, we can prepare a virtual environment to run Python 3 application that uses Pillow.
To do so, we first run the following command to create the virtual environment for running Python 3 applications:
virtualenv -p python3 PillowEnv
virtualenv had created the Python 3 virtual environment for us, we then activate the environment with the following command:
As a result of that, the current terminal session will run inside the virtual environment. Therefore, we can run the following command to install Pillow as a Python dependency for the virtual environment:
pip install pillow
pip command completes, we can run a Python 3 application that uses Pillow to process images from within the virtual environment.
Testing some capabilities of Pillow
Using the same terminal session, let's test some capabilities of Pillow.
Firstly, run the following command to get a large image into your Raspbian Stretch Lite:
wget -O large-file.jpg https://sites.google.com/site/tcperpetual/bigimage/1280px-LARGE_elevation.jpg
After the command completes, you will find a
large-file.jpg in the current working directory. This image is taken from https://commons.wikimedia.org/wiki/File:LARGE_elevation.jpg.
Thereafter, use the
nano editor to create a Python script:
nano editor loads, copy the following content into the editor:
from PIL import Image largeFileImage = Image.open('large-file.jpg') print(largeFileImage.format, largeFileImage.size, largeFileImage.mode)
After you had copied the contents, press 'Ctrl-X' and type 'Y' to save the file.
When you had saved the file, run the Python script with the following command:
You should see the following output in your terminal:
JPEG (1280, 640) RGB
With that, you can use the virtual environment to run Python 3 applications for image processing tasks on your Raspberry Pi.