Loading images into memory map

It is recommended to load large files into memory maps. Memory-mapped files only load a small part of large files. NumPy memory maps are array-like. In this example, we will generate an image of colored squares and load it into a memory map.

Getting ready

If necessary, install Matplotlib. The See Also section of this recipe has a reference to the corresponding recipe.

How to do it...

We will begin by initializing arrays.

  1. First, we need to initialize the following arrays:
    • an array that holds the image data
    • an array with random coordinates of the centers of the squares
    • an array with random radii of the squares
    • an array with random colors of the squares
      img = numpy.zeros((N, N), numpy.uint8)
      centers = numpy.random.random_integers(0, N, size=(NSQUARES, 2))
      radii = numpy.random.randint(0, N/9, size=NSQUARES)
      colors = numpy.random.randint(100, 255, size=NSQUARES)

    Initialize the arrays as follows:

    As you can see, we are initializing the first array to zeroes. The other arrays are initialized with functions from the numpy.random package that generate random integers.

  2. Generate squares.

    The next step is to generate squares. We create the squares using the arrays in the previous step. With the clip function, we will make sure that the squares do not wander outside the image area.

    The meshgrid function gives us the coordinates of the squares. If we give this function two arrays with size N and M, it will give us two arrays of shape N by M. The first array will have its elements repeated along the x axis. The second array will have its elements repeated along the y axis. The following example of an IPython session should make this clearer:

    In: x = linspace(1, 3, 3)
    
    In: x
    Out: array([ 1.,  2.,  3.])
    In: y = linspace(1, 2, 2)
    
    In: y
    Out: array([ 1.,  2.])
    
    In: meshgrid(x, y)
    Out: 
    [array([[ 1.,  2.,  3.],
          [ 1.,  2.,  3.]]),
     array([[ 1.,  1.,  1.],
          [ 2.,  2.,  2.]])]
    

    Finally, we will set the colors of the squares:

    for i in xrange(NSQUARES):
       xindices = range(centers[i][0] - radii[i], centers[i][0] + radii[i])
        xindices = numpy.clip(xindices, 0, N - 1)
        yindices = range(centers[i][1] - radii[i], centers[i][1] + radii[i])
        yindices = numpy.clip(yindices, 0, N - 1)
    
        if len(xindices) == 0 or len(yindices) == 0:
          continue
    
        coordinates = numpy.meshgrid(xindices, yindices)
        img[coordinates] = colors[i]
  3. Load into memory map.

    Before we load the image data into a memory map, we need to store it into a file with the tofile function. Then, we load the image data from this file into a memory map with the memmap function:

    img.tofile('random_squares.raw')
    img_memmap = numpy.memmap('random_squares.raw', shape=img.shape)
  4. Display the image.

    To demonstrate that everything worked fine, we will display the image with Matplotlib:

    matplotlib.pyplot.imshow(img_memmap)
    matplotlib.pyplot.axis('off')
    matplotlib.pyplot.show()

    Notice that we are not displaying the axes. The following is an example of a generated image:

    How to do it...

The following is the complete source code for this recipe:

import numpy
import matplotlib.pyplot
import sys

N = 512

if(len(sys.argv) != 2):
   print "Please input the number of squares to generate"
   sys.exit()

NSQUARES = int(sys.argv[1]) 

# Initialize
img = numpy.zeros((N, N), numpy.uint8)
centers = numpy.random.random_integers(0, N, size=(NSQUARES, 2))
radii = numpy.random.randint(0, N/9, size=NSQUARES)
colors = numpy.random.randint(100, 255, size=NSQUARES)

# Generate squares
for i in xrange(NSQUARES):
   xindices = range(centers[i][0] - radii[i], centers[i][0] + radii[i])
   xindices = numpy.clip(xindices, 0, N - 1)
   yindices = range(centers[i][1] - radii[i], centers[i][1] + radii[i])
   yindices = numpy.clip(yindices, 0, N - 1)

   if len(xindices) == 0 or len(yindices) == 0:
      continue

   coordinates = numpy.meshgrid(xindices, yindices)
   img[coordinates] = colors[i]

# Load into memory map
img.tofile('random_squares.raw')
img_memmap = numpy.memmap('random_squares.raw', shape=img.shape)


# Display image
matplotlib.pyplot.imshow(img_memmap)
matplotlib.pyplot.axis('off')
matplotlib.pyplot.show()

How it works...

We used the following functions in this recipe:

Function

Description

zeros

Gives an array filled with zeroes.

random_integers

Returns an array with random integer values between a high and low bound.

randint

Synonym for random_integers.

clip

Clips values of an array, given a minimum and a maximum.

meshgrid

Returns coordinate arrays from an array containing x-coordinates, and an array containing y-coordinates.

tofile

Writes an array to a file.

memmap

Creates a NumPy memory map from a file given the name of a file. Optionally, you can specify the shape of the array.

axis

Matplotlib function that configures the plot axes. For instance, we can turn them off.

See also

  • The Installing Matplotlib recipe in Chapter 1, Winding Along with IPython
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset