O'Reilly logo

NumPy Cookbook - Second Edition by Ivan Idris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Loading images into memory maps

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 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 (plural of radius) of the squares
    • An array with random colors of the squares

    Initialize the arrays:

    img = np.zeros((N, N), np.uint8) NSQUARES = 30 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required