NumPy · lesson 12 of 13
Parts in this lesson
Anp.save("week.npy", week)one array, to a file
Bnp.savez("stores.npz", kilos=kilos)several arrays, by name
Cimg[0:2, 2:5]2.5: a crop is a slice

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.savez and np.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.

save.ipynb
  • np.save("week.npy", week) writes one array to a .npy file. np.load gives 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.savetxt writes 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.

A picture as numbers. A tiny picture, 4 pixels high and 6 wide: sky, a sun and grass.img shape (4, 6, 3)
1 of 5 A tiny picture, 4 pixels high and 6 wide: sky, a sun and grass.
picture.ipynb
  • 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 (250+210+40)/3≈167.

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 (x,y): across first, then up. The sun is 4 across and 0 down. Run it.

sun.ipynb

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

You simulate a year of sales, which takes two minutes. How should you keep the result for tomorrow?
A photo has shape (600, 800, 3). How wide is it?
Which flips an image left to right?

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.

All sources for Simple Data Analysis