Transform images: flip, rotate, crop and resize
Change an image’s geometry and brightness by rearranging and adjusting its numbers.
- Flip, rotate and crop an image by rearranging its array
- Resize an image with nearest-neighbor sampling
- Adjust brightness without overflowing 0–255
Cropping a photo, rotating it, making a thumbnail, brightening a dark shot: every edit you make in a photo app is arithmetic on the pixel grid. Vision systems use the same operations to prepare images (resize everything to one size) and to create extra training data (flipped or brightened copies).
Try it
Image transformer
Apply operations to the letter F and compare the result with the original. Turn on Show pixel values to see what happens to the numbers.
- Is “flip left-right, then flip upside down” the same as rotating twice?
- Press Brighter several times. Why do the white pixels stop changing?
- Shrink to half, then Double size: is the original back? What was lost?
No operations yet.
Cropping is slicing
1image = [
2 [1, 2, 3],
3 [4, 5, 6],
4 [7, 8, 9],
5]
6crop = [row[1:] for row in image[1:]]
7print(crop)[[5, 6], [8, 9]]
image[1:] keeps rows from 1 on; row[1:] keeps columns from 1 on. A crop is a slice in both directions.
Resizing needs new pixel values. The simplest method, nearest neighbor, copies the nearest original pixel: fast and blocky. Smoother methods (bilinear, bicubic) blend neighboring pixels. Shrinking always throws information away, which is why enlarging a thumbnail never brings back the detail.
Key takeaways
Flips and rotations rearrange pixels; crops are slices of rows and columns.
Nearest-neighbor resizing copies the closest pixel - simple and blocky; shrinking loses detail for good.
Brightness changes must be clipped to 0–255.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: apply computer vision with Python
Use small pixel arrays to explore vision concepts, run your code against sample images, and connect each result to the larger computer vision idea.
Mirror an image
Read a JSON grayscale image and print it flipped left-to-right (a mirror image), as a list of rows.
- Two rows
- One column
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Brighten safely
Read a JSON grayscale image and, on the next line, an integer change (it can be negative). Add the change to every pixel, clip the results to 0–255, and print the image.
- Brighter
- Darker
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…