0
0
NumPydata~10 mins

Basic image manipulation with arrays in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Basic image manipulation with arrays
Load image as array
Access pixel values
Modify pixel values
Save or display modified image
We start by loading an image into an array, then access and change pixel values, and finally save or show the changed image.
Execution Sample
NumPy
import numpy as np
image = np.array([[100, 150], [200, 250]])
image[0, 1] = 255
print(image)
This code creates a small 2x2 image array, changes one pixel's brightness, and prints the updated array.
Execution Table
StepActionArray StatePixel ModifiedOutput
1Create 2x2 array[[100, 150], [200, 250]]None[[100 150] [200 250]]
2Change pixel at (0,1) to 255[[100, 255], [200, 250]](0,1)[[100 255] [200 250]]
3Print array[[100, 255], [200, 250]]None[[100 255] [200 250]]
💡 All steps completed, image array modified and printed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
imageundefined[[100 150] [200 250]][[100 255] [200 250]][[100 255] [200 250]]
Key Moments - 2 Insights
Why does changing image[0, 1] affect the array?
Because image is a numpy array, and image[0, 1] accesses the pixel at row 0, column 1. Changing it updates the array directly, as shown in step 2 of the execution table.
Why do we see the updated array printed after modification?
The print statement in step 3 outputs the current state of the array, which includes the modified pixel from step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of image[0,1] after step 2?
A100
B150
C255
D200
💡 Hint
Check the 'Array State' and 'Pixel Modified' columns in step 2.
At which step is the pixel value at (0,1) changed?
AStep 1
BStep 2
CStep 3
DNo change occurs
💡 Hint
Look at the 'Action' column describing pixel modification.
If we changed image[1,0] to 0 instead of image[0,1], what would be the value at image[1,0] after step 2?
A0
B200
C255
D250
💡 Hint
Think about which pixel is modified and check the 'Pixel Modified' column.
Concept Snapshot
Basic image manipulation with arrays:
- Load image as numpy array
- Access pixels by row and column indices
- Modify pixel values directly
- Print or save the updated array
- Changes affect the array immediately
Full Transcript
We start by loading an image as a numpy array. Each pixel is accessed by its row and column index. Changing a pixel value updates the array directly. Finally, printing the array shows the modified image data. This simple process allows basic image manipulation using arrays.