Open Multiple Images with OpenCV in Python

Building on our previous example of opening a single image, this guide will explain how to open multiple images. The easiest way to load multiple images is to put all the images into a single folder and loop through the directory opening each one.

This guide doesn’t introduce any new OpenCV functions you shouldn’t already be familiar with from the previous example, but we will include new packages os and os.path to make a list of images to process.

The code

A note on OpenCV image support from the OpenCV docs (http://docs.opencv.org/2.4/doc/tutorials/introduction/display_image/display_image.html)

Note OpenCV offers support for the image formats Windows bitmap (bmp), portable image formats (pbm, pgm, ppm) and Sun raster (sr, ras). With help of plugins (you need to specify to use them if you build yourself the library, nevertheless in the packages we ship present by default) you may also load image formats like JPEG (jpeg, jpg, jpe), JPEG 2000 (jp2 – codenamed in the CMake as Jasper), TIFF files (tiff, tif) and portable network graphics (png). Furthermore, OpenEXR is also a possibility.

Boilerplate code

 

Open an Image with OpenCV in Python

The following code will get you started to load and display an image with OpenCV and Python.

OpenCV functions used:

The code

 

Boilerplate code

 

Where to next?

Install OpenCV 3.x with Python 2.7 on Ubuntu

Updated: 10/11/2016

How to Install OpenCV 3.x and Python 2.7+ on Ubuntu

Step 1:

Open a terminal window and update the apt package manager lists and upgrade any currently installed packages:

Step 2:

Install developer tools:

Step 3:

Install packages to open image formats including JPEG, PNG, TIFF, and others:

Step 4:

Install the GIMP toolkit (GTK+) which OpenCV uses to build GUIs:

Step 5:

Install packages to open video formats:

Step 6:

Install libraries that optimize various routines in OpenCV:

Step 7:

Install pip, a package manager for Python:

https://pip.pypa.io/ is the official pip website

Step 8:

Install Python 2.7 development tools:

Step 9:

Install numpy because OpenCV stores images as NumPy arrays

This post follows, updates and adds to the instructions from Py Image Search here: http://www.pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/