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.
If necessary, install Matplotlib. The See Also section of this recipe has a reference to the corresponding recipe.
We will begin by initializing arrays.
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.
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]
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)
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:
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()
We used the following functions in this recipe: