Setting up different image processing libraries in Python
The next few paragraphs describe to install different image processing libraries and set up the environment for writing codes to process images using classical image processing techniques in Python. In the last few chapters of this book, we will need to use a different setup when we use deep-learning-based methods.
Â
Installing pip
We are going to use the pip
(orpip3
) tool to install the libraries, so—if it isn't already installed—we need to install pip
 first. As mentioned here (https://pip.pypa.io/en/stable/installing/#do-i-need-to-install-pip), pip
 is already installed if we are using Python 3 >=3.4 downloaded from python.org, or if we are working in a Virtual Environment (https://packaging.python.org/tutorials/installing-packages/#creating-and-using-virtual-environments) created by virtualenv
(https://packaging.python.org/key_projects/#virtualenv) or pyvenv
(https://packaging.python.org/key_projects/#venv). We just need to make sure to upgrade pip
(https://pip.pypa.io/en/stable/installing/#upgrading-pip). How to install pip
for different OSes or platforms can be found here: https://stackoverflow.com/questions/6587507/how-to-install-pip-with-python-3.
Installing some image processing libraries in Python
In Python, there are many libraries that we can use for image processing. The ones we are going to use are: NumPy, SciPy, scikit-image, PIL (Pillow), OpenCV, scikit-learn, SimpleITK, and Matplotlib.
The matplotlib
library will primarily be used for display purposes, whereas numpy
will be used for storing an image. The scikit-learn
library will be used for building machine-learning models for image processing, and scipy
will be used mainly for image enhancements. The scikit-image
, mahotas
, and opencv
 libraries will be used for different image processing algorithms.
The following code block shows how the libraries that we are going to use can be downloaded and installed with pip
from a Python prompt (interactive mode):
>>> pip install numpy >>> pip install scipy >>> pip install scikit-image >>> pip install scikit-learn >>> pip install pillow >>> pip install SimpleITK >>> pip install opencv-python >>> pip install matplotlib
Â
Â
Â
Â
There may be some additional installation instructions, depending on the OS platform you are going to use. We suggest the reader goes through the documentation sites for each of the libraries to get detailed platform-specific installation instructions for each library. For example, for the scikit-image
library, detailed installation instructions for different OS platforms can be found here: http://scikit-image.org/docs/stable/install.html. Also, the reader should be familiar with websites such as stackoverflow to resolve platform-dependent installation issues for different libraries.
Finally, we can verify whether a library is properly installed or not by importing it from the Python prompt. If the library is imported successfully (no error message is thrown), then we don't have any installation issue. We can print the version of the library installed by printing it to the console.
The following code block shows the versions for the scikit-image
and PIL
 Python libraries:Â
>>> import skimage, PIL, numpy >>> print(skimage.__version__) # 0.14.0 >>> PIL.__version__ # 5.1.0 >>> numpy.__version__ # 1.14.5
Let us ensure that we have the latest versions of all of the libraries.
Installing the Anaconda distribution
We also recommend to download and install the latest version of the Anaconda distribution; this will eliminate the need for explicit installation of many Python packages.Â
Note
More about installing Anaconda for different OSes can be found at https://conda.io/docs/user-guide/install/index.html.
Â
Â
Installing Jupyter Notebook
We are going to use Jupyter notebooks to write our Python code. So, we need to install thejupyter
package first from a Python prompt with >>> pip install jupyter
, and then launch the Jupyter Notebook app in the browser using >>> jupyter notebook
. From there, we can create new Python notebooks and choose a kernel. If we use Anaconda, we do not need to install Jupyter explicitly; the latest Anaconda distribution comes with Jupyter.
Note
More about running Jupyter notebooks can be found at http://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/execute.html.
We can even install a Python package from inside a notebook cell; for example, we can installscipy
with the !pip install scipy
 command.
Note
For more information on installing Jupyter, please refer to http://jupyter.readthedocs.io/en/latest/install.html.