Exercise 03, "zoom on me", combines the two previous ideas. Loading gave you an image as an array; slicing let you carve a sub-region. "Zooming" is exactly that — crop a region of interest, then display it large. No interpolation magic, just slicing and a plotting call.
Zoom = crop a sub-array
To zoom into the top-left 400×400 region, you slice the first 400 rows and the first 400 columns:
import numpy as np
from load_image import ft_load
image = ft_load("animal.jpeg") # shape e.g. (768, 1024, 3)
print("The shape of image is:", image.shape)
zoom = image[100:500, 400:800] # 400x400 region of interest
print("New shape after slicing:", zoom.shape)
The two slice ranges pick the rows and columns of the window; everything outside is simply dropped. Because slicing is a view, this costs almost nothing — you are not resampling pixels, just choosing which ones to look at.
Dropping to a single channel
The expected output shows a grayscale-looking crop with shape (400, 400, 1) or (400, 400). You can keep one channel to make the array 2D:
zoom = image[100:500, 400:800, 0] # take channel 0 -> (400, 400)
# or keep the trailing axis to stay 3D:
zoom = image[100:500, 400:800, 0:1] # -> (400, 400, 1)
Slicing the third axis the same way you slice the first two — this is the multi-axis slicing from exercise 01 used in anger. , 0 removes the axis; , 0:1 keeps it with length 1.
Displaying with matplotlib and scaled axes
The subject wants the image shown with the scale on the x and y axes. That is matplotlib's default behavior — imshow labels the pixel coordinates automatically:
import matplotlib.pyplot as plt
plt.imshow(zoom, cmap="gray") # cmap matters for single-channel data
plt.title("Zoomed image")
plt.show()
A couple of points:
- For a 2D (single-channel) array you must pass a colormap such as
cmap="gray", otherwise matplotlib applies its default false-color map. - The axis ticks (0, 50, 100…) are the pixel indices of the cropped region — that is the "scale" the subject refers to.
- The origin is the top-left, so the y-axis counts downward, matching image-row order.
Print the information
The exercise asks you to report the size on X and Y, the number of channels, and the pixel content. All of it comes straight from the array:
print("Size X (width):", image.shape[1])
print("Size Y (height):", image.shape[0])
print("Channels:", image.shape[2] if image.ndim == 3 else 1)
print(image) # the pixel content (NumPy truncates with ... )
Robustness
"If anything went wrong, the program must not stop abruptly." Wrap the load and the slicing: a missing file, or slice bounds larger than the image, should print a clear message instead of raising. An out-of-range slice in NumPy does not error (it just clamps), but a missing file does — so the try/except around ft_load remains your safety net.
Takeaways
- Zoom is crop:
image[r0:r1, c0:c1]selects a region of interest. - Slice the channel axis too —
, 0drops it,, 0:1keeps it. plt.imshowrenders the array and labels pixel-coordinate axes; passcmap="gray"for single-channel data.- All reported info (width, height, channels) is read directly from
shape.