(For more resources related to this topic, see here.)
Installing SciPy
SciPy is the scientific Python library and is closely related to NumPy. In fact, SciPy and NumPy used to be one and the same project many years ago. In this recipe, we will install SciPy.
How to do it…
In this recipe, we will go through the steps for installing SciPy.

Installing from source: If you have Git installed, you can clone the SciPy repository using the following command:
git clone https://github.com/scipy/scipy.git
python setup.py build
python setup.py install userThis installs to your home directory and requires Python 2.6 or higher.
Before building, you will also need to install the following packages on which SciPy depends:

BLAS and LAPACK libraries

C and Fortran compilers
There is a chance that you have already installed this software as a part of the NumPy installation.


Installing SciPy on Linux: Most Linux distributions have SciPy packages. We will go through the necessary steps for some of the popular Linux distributions:

In order to install SciPy on Red Hat, Fedora, and CentOS, run the following instructions from the command line:
yum install pythonscipy

In order to install SciPy on Mandriva, run the following command line instruction:
urpmi pythonscipy

In order to install SciPy on Gentoo, run the following command line instruction:
sudo emerge scipy

On Debian or Ubuntu, we need to type the following:
sudo aptget install pythonscipy


Installing SciPy on Mac OS X: Apple Developer Tools (XCode) is required, because it contains the BLAS and LAPACK libraries. It can be found either in the App Store, or in the installation DVD that came with your Mac, or you can get the latest version from Apple Developer’s connection at https://developer.apple.com/technologies/tools/. Make sure that everything, including all the optional packages is installed.
You probably already have a Fortran compiler installed for NumPy. The binaries for gfortran can be found at http://r.research.att.com/tools/.

Installing SciPy using easy_install or pip: Install with either of the following two commands:
sudo pip install scipy
easy_install scipy 
Installing on Windows: If you have Python installed already, the preferred method is to download and use the binary distribution. Alternatively, you may want to install the Enthought Python distribution, which comes with other scientific Python software packages.

Check your installation: Check the SciPy installation with the following code:
import scipy print scipy.__version__ print scipy.__file__
This should print the correct SciPy version.
How it works…
Most package managers will take care of any dependencies for you. However, in some cases, you will need to install them manually. Unfortunately, this is beyond the scope of this book. If you run into problems, you can ask for help at:

The #scipy IRC channel of freenode, or

The SciPy mailing lists at http://www.scipy.org/Mailing_Lists
Installing PIL
PIL, the Python imaging library, is a prerequisite for the image processing recipes in this article.
How to do it…
Let’s see how to install PIL.

Installing PIL on Windows: Install using the Windows executable from the PIL website http://www.pythonware.com/products/pil/.

Installing on Debian or Ubuntu: On Debian or Ubuntu, install PIL using the following command:
sudo aptget install pythonimaging

Installing with easy_install or pip: At the t ime of writing this book, it appeared that the package managers of Red Hat, Fedora, and CentOS did not have direct support for PIL. Therefore, please follow this step if you are using one of these Linux distributions.
Install with either of the following commands:
easy_install PIL
sudo pip install PIL
Resizing images
In this recipe, we will load a sample image of Lena, which is available in the SciPy distribution, into an array. This article is not about image manipulation, by the way; we will just use the image data as an input.
Lena Soderberg appeared in a 1972 Playboy magazine. For historical reasons, one of those images is often used in the field of image processing. Don’t worry; the picture in question is completely safe for work.
We will resize the image using the repeat function. This function repeats an array, which in practice means resizing the image by a certain factor.
Getting ready
A prerequisite for this recipe is to have SciPy, Matplotlib, and PIL installed.
How to do it…

Load the Lena image into an array.
SciPy has a lena function , which can load the image into a NumPy array:
lena = scipy.misc.lena()
Some refactoring has occurred since version 0.10, so if you are using an older version, the correct code is:
lena = scipy.lena()

Check the shape.
Check the shape of the Lena array using the assert_equal function from the numpy.testing package—this is an optional sanity check test:
numpy.testing.assert_equal((LENA_X, LENA_Y), lena.shape)

Resize the Lena array.
Resize the Lena array with the repeat function. We give this function a resize factor in the x and y direction:
resized = lena.repeat(yfactor, axis=0).repeat(xfactor, axis=1)

Plot the arrays.
We will plot the Lena image and the resized image in two subplots that are a part of the same grid. Plot the Lena array in a subplot:
matplotlib.pyplot.subplot(211) matplotlib.pyplot.imshow(lena)
The Matplotlib subplot function creates a subplot. This function accepts a 3digit integer as the parameter, where the first digit is the number of rows, the second digit is the number of columns, and the last digit is the index of the subplot starting with 1. The imshow function shows images. Finally, the show function displays the end result.
Plot the resized array in another subplot and display it. The index is now 2:
matplotlib.pyplot.subplot(212) matplotlib.pyplot.imshow(resized) matplotlib.pyplot.show()
The following screenshot is the result with the original image (first) and the resized image (second):
The following is the complete code for this recipe:
import scipy.misc import sys import matplotlib.pyplot import numpy.testing # This script resizes the Lena image from Scipy. if(len(sys.argv) != 3): print "Usage python %s yfactor xfactor" % (sys.argv[0]) sys.exit() # Loads the Lena image into an array lena = scipy.misc.lena() #Lena's dimensions LENA_X = 512 LENA_Y = 512 #Check the shape of the Lena array numpy.testing.assert_equal((LENA_X, LENA_Y), lena.shape) # Get the resize factors yfactor = float(sys.argv[1]) xfactor = float(sys.argv[2]) # Resize the Lena array resized = lena.repeat(yfactor, axis=0).repeat(xfactor, axis=1) #Check the shape of the resized array numpy.testing.assert_equal((yfactor * LENA_Y, xfactor * LENA_Y), resized.shape) # Plot the Lena array matplotlib.pyplot.subplot(211) matplotlib.pyplot.imshow(lena) #Plot the resized array matplotlib.pyplot.subplot(212) matplotlib.pyplot.imshow(resized) matplotlib.pyplot.show()
How it works…
The repeat function repeats arrays, which, in this case, resulted in changing the size of the original image. The Matplotlib subplot function creates a subplot. The imshow function shows images. Finally, the show function displays the end result.
See also

The Installing SciPy recipe

The Installing PIL recipe