Saving arrays, and images as arrays
Last updated
Two last things before the project. First, how to save an array and load it back, so a long calculation only runs once. Second, a picture is just an array too: every photo you'll ever analyse is numbers in a grid. In this lesson you'll save and load arrays, then crop, flip and grey an image with the slices you already know.
By the end of this lesson you can
- Save and load arrays with
np.save,np.savezandnp.load - Explain why an image is an array of shape
(height, width, 3) - Crop, flip and grey an image with slices and
mean
Saving and loading
Run the cells. They save files into the notebook's own folder, and load them straight back.
np.save("week.npy", week)writes one array to a.npyfile.np.loadgives it back exactly: the same numbers, shape and dtype.np.savez("stores.npz", kilos=kilos, week=week)puts several arrays in one file, each with a name. Load it, and pick them out by name like a dictionary.np.savetxtwrites plain text, one number per line here (fmt="%d"means "as whole numbers"). Any program can read it, but it's slower and bigger, and a table loses its shape unless you say how to lay it out.
When to use which: .npy and .npz to save your own work between sessions, such as a simulation that took a minute to run. Text or CSV when someone else, or another program, needs the numbers. For tables with column names, pandas has better formats, in lesson 3.3.
An image is an array
Step through the picture, then run the cells. They build the same tiny image by hand.
- The shape is
(4, 6, 3): 4 rows of pixels, 6 columns, and 3 colour values for each. Every photo works this way; a phone photo might be(3000, 4000, 3). - The dtype is
uint8: whole numbers from 0 to 255, one byte each. "u" means unsigned: no negatives. img[0, 4]is the pixel in row 0, column 4: the sun,[250, 210, 40], lots of red and green and little blue, which is yellow.- Cropping is slicing, and flipping is slicing with a step of −1. After
img[:, ::-1]the sun is at column 1. img.mean(axis=2)averages each pixel's three colours into one number: a greyscale image,(4, 6). The sun's grey is .
Everything from lessons 2.4 to 2.8 works on images unchanged: that's why image-processing and machine-learning libraries all take NumPy arrays.
Wrong way: x and y
In maths you write a point as : across first, then up. The sun is 4 across and 0 down. Run it.
IndexError: index 4 is out of bounds for axis 0 with size 4. An image array is rows first: img[row, column], which is img[y, x]. img[4, 0] asked for row 4, and there are only rows 0 to 3.
This one is easy to miss when it doesn't fail. In a square image, img[x, y] gives a pixel every time, just the wrong one, mirrored across the diagonal. Fix the cell to img[y, x]. A good habit is to name things row and col rather than x and y when you index arrays, so the order reads correctly.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- NumPy: numpy.save: the .npy format
- NumPy: numpy.savez: several arrays in one .npz file
- NumPy: numpy.savetxt: an array as text, and the fmt argument
- van der Walt et al., scikit-image: image processing in Python, PeerJ 2:e453, 2014: images as NumPy arrays, rows first