📖 Scenario: Imagine you have a small image represented by pixels. Each pixel has a color made of red, green, and blue parts. We will use numbers to show these colors in a table-like structure called an array.
🎯 Goal: You will create a small image as a 3D array using numpy, then select a color channel and finally print the selected color channel array.
📋 What You'll Learn
Use numpy to create a 3D array representing an image
Create a variable for the image array with exact pixel values
Create a variable to select the red color channel
Print the red channel array
💡 Why This Matters
🌍 Real World
Images in computers are stored as arrays of numbers representing colors. Understanding this helps in editing, filtering, and analyzing images.
💼 Career
Data scientists and machine learning engineers often work with image data. Knowing how to manipulate image arrays is key for tasks like image recognition and computer vision.
Progress0 / 4 steps
1
Create the image array
Create a variable called image using numpy.array with these exact pixel values representing a 2x2 image: [[[255, 0, 0], [0, 255, 0]], [[0, 0, 255], [255, 255, 0]]]. This means top-left pixel is red, top-right is green, bottom-left is blue, bottom-right is yellow.
NumPy
Hint
Use np.array to create a 3D array with shape (2, 2, 3). Each inner list is a pixel with RGB values.
2
Select the red color channel
Create a variable called red_channel that selects the red color values from the image array. Use array slicing to get all rows and columns but only the first color value (index 0) for red.
NumPy
Hint
Use image[:, :, 0] to get all rows and columns but only the red color values.
3
Check the shape of the red channel
Create a variable called shape_red that stores the shape of the red_channel array using the .shape attribute.
NumPy
Hint
Use red_channel.shape to find the dimensions of the red channel array.
4
Print the red channel array
Print the red_channel array to see the red color values of each pixel.
NumPy
Hint
Use print(red_channel) to display the red values.
Practice
(1/5)
1. What does each element in a NumPy array representing an image usually stand for?
easy
A. A pixel's color value
B. The image file size
C. The image format type
D. The number of images
Solution
Step 1: Understand image representation in arrays
Images are stored as arrays where each element corresponds to a pixel's color information.
Step 2: Identify what each element represents
Each element holds the color value (like red, green, or blue intensity) for that pixel.
Final Answer:
A pixel's color value -> Option A
Quick Check:
Pixel color = element value [OK]
Hint: Pixels map to array elements holding color values [OK]
Common Mistakes:
Confusing pixel values with image size
Thinking array stores image format
Assuming array length equals number of images
2. Which NumPy code correctly creates a 3x3 image array with 3 color channels (RGB)?
easy
A. np.zeros((3, 3, 3))
B. np.zeros([3, 3])
C. np.zeros(3, 3, 3)
D. np.zeros((3, 3))
Solution
Step 1: Understand array shape for RGB image
An RGB image array shape is (height, width, 3) where 3 is for color channels.
Step 2: Check correct NumPy syntax for shape
NumPy expects shape as a tuple inside zeros(), so (3, 3, 3) is correct.
Final Answer:
np.zeros((3, 3, 3)) -> Option A
Quick Check:
Shape tuple with 3 channels = np.zeros((3,3,3)) [OK]
Hint: Use tuple shape with 3 for RGB channels [OK]
Common Mistakes:
Passing shape as separate arguments instead of tuple
Missing the third dimension for color channels
Using list instead of tuple for shape
3. What is the output shape of this code?
import numpy as np
img = np.ones((4, 5, 3))
print(img.shape)
medium
A. (5, 4, 3)
B. (4, 5, 3)
C. (4, 5)
D. (3, 4, 5)
Solution
Step 1: Analyze the array creation
np.ones((4, 5, 3)) creates an array with shape (4, 5, 3).
Step 2: Understand shape attribute output
Printing img.shape returns the tuple describing the array dimensions exactly as given.
Final Answer:
(4, 5, 3) -> Option B
Quick Check:
Shape matches input tuple (4,5,3) [OK]
Hint: Shape prints exactly as array was created [OK]
Common Mistakes:
Mixing up width and height order
Ignoring the color channel dimension
Assuming shape returns total size
4. Find the error in this code that tries to create a 2x2 RGB image array:
import numpy as np
img = np.zeros(2, 2, 3)
print(img.shape)
medium
A. np.zeros cannot create 3D arrays
B. Shape dimensions are too large
C. Missing tuple for shape in np.zeros
D. print statement syntax is wrong
Solution
Step 1: Check np.zeros syntax
np.zeros expects a single shape argument as a tuple, but here three separate arguments are given.
Step 2: Identify correct shape input
Shape should be passed as np.zeros((2, 2, 3)) with parentheses around the shape tuple.
Final Answer:
Missing tuple for shape in np.zeros -> Option C
Quick Check:
Shape must be tuple in np.zeros() [OK]
Hint: Always pass shape as one tuple to np.zeros [OK]