In this recipe, we will load a sample image of Lena, which is available in the SciPy distribution, into an array. This chapter is not about image manipulation, by the way; we will just use the image data as an input.
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.
A prerequisite for this recipe is to have SciPy, Matplotlib, and PIL installed. Have a look at the corresponding recipes in this chapter and the previous chapter.
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 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 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)
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 3-digit 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()
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.