Python for Computer Vision

This is a quick installation guide that will show you how to set up a programming environment for writing computer vision algorithms in Python. You'll install Python, an IDE, and some supporting libraries. This guide is mostly cross-platform, with some emphasis on OS X.

You will need:

  • Python 3.x (3.3 at time of writing)1
  • Python libraries for common vision & scientific computing tasks
  • OpenCV (optional)
  • Eclipse with PyDev (optional but recommended)

Here are the Python libraries that you will use:

  • Python Imaging Library (PIL)
  • NumPy
  • matplotlib

And here are a couple additional ones which are optional, but you'll probably find them useful sooner or later:

  • SciPy
  • scikit-image
  • ipython

To install them you will use pip and virtualenv.2

Python and Assorted Libraries

You likely already have Python on your computer. But if you are on a Mac, I recommend for you to use Homebrew to manage your Python installations.

$ brew install python3 # Using Python 3, but you can also use Python 2.

If you don't already have pip, install it now (if you're using Homebrew, this should already have been done for you):

$ curl -O https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py

If you don't already have virtualenv, install it now:

$ sudo pip3 install virtualenv # Use 'pip' for Python 2, 'pip3' for Python 3
$ sudo pip3 install virtualenvwrapper

You could at this point try installing your Python packages, but you may have some missing dependencies.

On OS X, I needed to perform the following installations first (note that freetype may already exist somewhere, but needs to be symlinked to the correct location):3

$ brew install freetype # required by PIL
$ ln -s /usr/local/include/freetype2 /usr/local/include/freetype # only on OS X; see footnote 3
$ brew install swig # required by scipy

On Linux, I needed to perform the following installations first (note that there are alternative choices for all of these dependencies; you just need some version of BLAS and LAPACK and a Fortran compiler):

$ sudo apt-get install libblas-dev # required by scipy
$ sudo apt-get install liblapack-dev # required by scipy
$ sudo apt-get install gfortran # required by scipy

Now you should be ready to install your cool Python tools!

Linux users: You may be able to skip part of the following step, because the major packages are often shipped with Linux distributions. It can't hurt to install the latest version, but you don't need to if you don't want to. Find out what's already installed with pip list. Find out if newer versions are available with pip list --outdated.

# This will automatically switch you into the new virtualenv so you can start installing packages.
# Your new virtualenv will be called "vision".
# You can exclude the "-p `which python3`" if you don't want to use Python 3.
$ mkvirtualenv -p `which python3` vision
$ pip install Pillow # see footnote 4
$ pip install numpy
$ pip install matplotlib

# And the optional packages:
$ pip install scipy
$ pip install ipython
$ pip install cython
$ pip install scikit-image

# confirm that everything worked
$ pip list

That's it! You're all ready to go with your next-generation Python algorithms for computer vision! If you additionally want to install OpenCV, see my separate post about that. If you don't yet have a Python development environment, do read my post on PyDev and virtualenv.


  1. I'm using Python 3 here. If you know anything about Python, you'll have heard how much confusion there is around Python 2 vs. Python 3. You can also use Python 2, but the entire NumPy/SciPy ecosystem has supported Python 3 for a couple years now, so you should be safe to prefer 3. Homebrew manages 2 and 3 as completely separate packages. You can have both simultaneously installed on your Mac, and 'python' will always refer to Python 2, while 'python3' will always refer to Python 3. The only hitch is you will have to remember to specify python3 for your virtualenv, and use pip3 to install global libraries for Python 3. If you don't understand what that means, just forget I even said it; I've written my instructions to do things the Python 3 way.
  2. If you need an introduction to Python's packaging system, see this page. TL;DR: pip lets you install packages (Python libraries). Usually, you do not want to install packages globally, for the entire system, because they may conflict with each other. Instead, you want each Python project you create to have its own isolated ecosystem. Virtualenv provides this isolation. Virtualenvwrapper makes virtualenv nicer to use.
  3. I'm not sure why Freetype is in a different location on OS X than on Linux, but I guess this is the location that Xcode decided upon and Homebrew follows suit. So we just create a quick symlink and hopefully never have to worry about it again.
  4. While writing this post I discovered a new package for the Python Imaging Library. It seems that support for PIL is waning, and is not available via pip by default. It might someday regain favor, but I find Pillow to be better supported at the moment.
Tagged , , , , , . Bookmark the permalink.

One Response to Python for Computer Vision

  1. Adrian says:

    I normally end up using Vagrant to custom configure my environment (and allow it to be more portable if I need to ship it off to a client), but I love the virtualenv approach. Great tutorial, keep it up!

Leave a Reply

Your email address will not be published. Required fields are marked *