0
0
NumPydata~15 mins

Image as array concept in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Image as array concept
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(red_channel) to display the red values.