0
0
NumPydata~10 mins

Image as array concept in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Image as array concept
Load Image File
Convert to Array
Array Shape: Height x Width x Channels
Access/Modify Pixels
Use Array for Analysis or Display
This flow shows how an image file is loaded and converted into a numeric array, where each pixel's color is stored as numbers in a 3D array.
Execution Sample
NumPy
import numpy as np
image = np.array([[[255,0,0],[0,255,0]],
                  [[0,0,255],[255,255,0]]])
print(image.shape)
print(image[0,1])
Create a small 2x2 image array with RGB colors, then print its shape and the color of one pixel.
Execution Table
StepActionArray ShapeAccessed PixelOutput
1Create 2x2 RGB image array(2, 2, 3)N/AArray created with pixel colors
2Print array shape(2, 2, 3)N/A(2, 2, 3)
3Access pixel at row 0, col 1(2, 2, 3)[0, 1][0, 255, 0] (Green pixel)
4End of code(2, 2, 3)N/AExecution complete
💡 All steps executed; image array created and pixel accessed successfully
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
imageundefined[[[255,0,0],[0,255,0]], [[0,0,255],[255,255,0]]][[[255 0 0] [ 0 255 0]] [[ 0 0 255] [255 255 0]]] shape (2,2,3)[[[255 0 0] [ 0 255 0]] [[ 0 0 255] [255 255 0]]] shape (2,2,3)Same as after Step 3
Key Moments - 3 Insights
Why does the image array have three numbers for each pixel?
Each pixel stores three values representing Red, Green, and Blue colors. This is shown in the execution_table at Step 3 where pixel [0,1] has [0, 255, 0], meaning full green.
What does the shape (2, 2, 3) mean for the image array?
It means the image has 2 rows (height), 2 columns (width), and 3 color channels (RGB). This is confirmed in execution_table Step 2.
How do we access a specific pixel's color in the array?
By using two indices for row and column, then the third index for color channel if needed. In the example, image[0,1] accesses the pixel at first row, second column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the shape of the image array after Step 2?
A(2, 2, 3)
B(3, 2, 2)
C(2, 3, 2)
D(3, 3, 3)
💡 Hint
Check the 'Array Shape' column in execution_table row for Step 2
At Step 3, what color does the pixel at position [0,1] represent?
ARed
BBlue
CGreen
DYellow
💡 Hint
Look at the 'Output' column for Step 3 in execution_table
If we change the pixel at [1,0] to [255,255,255], what would the array represent there?
ABlack pixel
BWhite pixel
CBlue pixel
DRed pixel
💡 Hint
Recall RGB values: [255,255,255] means full intensity of all colors, which is white
Concept Snapshot
Image as array concept:
- Images are stored as 3D arrays: Height x Width x Channels
- Each pixel has 3 values for Red, Green, Blue
- Access pixels by row and column indices
- Modify array to change image pixels
- Use numpy arrays for easy image data handling
Full Transcript
This lesson shows how images are represented as arrays in numpy. We start by creating a small 2x2 image with RGB colors. The array shape is (2, 2, 3), meaning 2 rows, 2 columns, and 3 color channels. Each pixel is a list of three numbers representing red, green, and blue intensities. We access a pixel by specifying its row and column. For example, image[0,1] gives the green pixel [0, 255, 0]. This numeric representation allows us to analyze or modify images easily using array operations.