Bird
Raised Fist0
NumPydata~20 mins

Basic image manipulation with arrays in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Image Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output shape after flipping the image array?
Given a 3D NumPy array representing an RGB image with shape (100, 200, 3), what will be the shape of the array after applying np.flipud(image)?
NumPy
import numpy as np
image = np.zeros((100, 200, 3))
flipped = np.flipud(image)
print(flipped.shape)
A(100, 3, 200)
B(100, 200, 3)
C(3, 100, 200)
D(200, 100, 3)
Attempts:
2 left
💡 Hint
Flipping up-down reverses rows but does not change the shape.
❓ data_output
intermediate
1:30remaining
What is the pixel value after increasing brightness?
Given a grayscale image array with pixel value 120, what will be the new pixel value after adding 50 and clipping to 255?
NumPy
import numpy as np
pixel = np.array([120])
bright_pixel = np.clip(pixel + 50, 0, 255)
print(bright_pixel[0])
A170
B255
C120
D50
Attempts:
2 left
💡 Hint
Adding 50 to 120 gives 170, which is less than 255, so clipping does not change it.
❓ visualization
advanced
2:30remaining
Which option shows the correct code to convert an RGB image to grayscale?
You have an RGB image array with shape (height, width, 3). Which code correctly converts it to a grayscale image using the luminosity method?
NumPy
import numpy as np

# image is a numpy array with shape (height, width, 3)
Agrayscale = np.dot(image[..., :3], [0.2989, 0.5870, 0.1140])
Bgrayscale = np.mean(image, axis=2)
Cgrayscale = image[:, :, 0] * 0.3 + image[:, :, 1] * 0.59 + image[:, :, 2] * 0.11
Dgrayscale = np.sum(image, axis=2) / 3
Attempts:
2 left
💡 Hint
The luminosity method uses weighted sums of RGB channels.
🔧 Debug
advanced
2:00remaining
What error does this code raise when trying to invert a binary image?
Consider this code snippet to invert a binary image (0s and 1s):
inverted = 1 - image
If image is a NumPy array of dtype bool, what error occurs?
NumPy
import numpy as np
image = np.array([[True, False], [False, True]], dtype=bool)
inverted = 1 - image
ATypeError: unsupported operand type(s) for -: 'bool' and 'bool'
BValueError: operands could not be broadcast together
CNo error, output is a boolean array inverted correctly
DTypeError: unsupported operand type(s) for -: 'int' and 'bool'
Attempts:
2 left
💡 Hint
Subtracting a boolean array from an integer can cause type errors.
🚀 Application
expert
3:00remaining
How many pixels change after thresholding an image?
Given a grayscale image array of shape (50, 50) with pixel values from 0 to 255, you apply thresholding:
thresholded = (image > 128).astype(int)
If exactly 1200 pixels have values greater than 128, how many pixels in thresholded are 1?
NumPy
import numpy as np
np.random.seed(0)
image = np.random.randint(0, 256, (50, 50))
thresholded = (image > 128).astype(int)
count_ones = np.sum(thresholded)
print(count_ones)
A2500
B1300
C1200
D128
Attempts:
2 left
💡 Hint
Thresholding sets pixels > 128 to 1, so count of 1s equals count of pixels > 128.

Practice

(1/5)
1. What does a 2D numpy array represent when used for a grayscale image?
easy
A. Pixel brightness values arranged in rows and columns
B. A list of image file names
C. The color channels of an image
D. The size of the image in bytes

Solution

  1. Step 1: Understand image representation

    A grayscale image is made of pixels arranged in rows and columns, each pixel having a brightness value.
  2. Step 2: Connect to numpy array structure

    A 2D numpy array stores these brightness values in a grid matching the image layout.
  3. Final Answer:

    Pixel brightness values arranged in rows and columns -> Option A
  4. Quick Check:

    Grayscale image = 2D pixel brightness array [OK]
Hint: Grayscale images use 2D arrays for pixel brightness [OK]
Common Mistakes:
  • Confusing pixel values with file names
  • Thinking 2D arrays store color channels
  • Mixing image size with pixel data
2. Which numpy code correctly flips a 2D image array img vertically?
easy
A. np.fliplr(img)
B. np.flip(img, axis=1)
C. np.flip(img, axis=0)
D. np.flip(img)

Solution

  1. Step 1: Understand vertical flip axis

    Vertical flip reverses rows, which corresponds to axis 0 in numpy arrays.
  2. Step 2: Match numpy functions to axis

    np.flip(img, axis=0) flips vertically.
  3. Final Answer:

    np.flip(img, axis=0) -> Option C
  4. Quick Check:

    Vertical flip = axis 0 flip [OK]
Hint: Vertical flip uses axis=0 in np.flip [OK]
Common Mistakes:
  • Using axis=1 flips horizontally
  • np.flip(img) without axis reverses all axes
  • Forgetting axis parameter in np.flip
3. Given this code:
import numpy as np
img = np.array([[10, 20], [30, 40]])
new_img = img + 5
print(new_img)

What is the output?
medium
A. [[10 20] [30 40]]
B. [[5 15] [25 35]]
C. [[50 60] [70 80]]
D. [[15 25] [35 45]]

Solution

  1. Step 1: Understand numpy array addition

    Adding 5 to a numpy array adds 5 to every element (broadcasting).
  2. Step 2: Calculate new array values

    Each element increases by 5: 10+5=15, 20+5=25, 30+5=35, 40+5=45.
  3. Final Answer:

    [[15 25] [35 45]] -> Option D
  4. Quick Check:

    Array + 5 adds 5 to all elements [OK]
Hint: Adding scalar adds to all pixels in array [OK]
Common Mistakes:
  • Thinking addition changes shape
  • Adding 5 only to first row or column
  • Confusing addition with multiplication
4. What is wrong with this code to crop the top-left 2x2 part of a 3x3 image array img?:
cropped = img[0:2, 0:3]
medium
A. It crops 2 rows but 3 columns, not 2 columns
B. It causes an index error
C. It flips the image instead of cropping
D. It returns the whole image

Solution

  1. Step 1: Analyze slicing indices

    The slice img[0:2, 0:3] selects rows 0 and 1 (2 rows) and columns 0,1,2 (3 columns).
  2. Step 2: Compare to desired crop size

    We want a 2x2 crop, but this selects 2x3, so it includes one extra column.
  3. Final Answer:

    It crops 2 rows but 3 columns, not 2 columns -> Option A
  4. Quick Check:

    Slicing columns 0:3 selects 3 columns [OK]
Hint: Check slice ranges match desired rows and columns [OK]
Common Mistakes:
  • Assuming slicing is exclusive of end index
  • Expecting error from out-of-range slice
  • Confusing rows and columns order
5. You have a color image as a 3D numpy array img with shape (100, 100, 3). How do you create a new image gray_img by averaging the color channels for each pixel?
hard
A. gray_img = img.sum(axis=0)
B. gray_img = img.mean(axis=2)
C. gray_img = img[:, :, 0]
D. gray_img = np.flip(img, axis=1)

Solution

  1. Step 1: Understand image shape and channels

    The shape (100, 100, 3) means 100x100 pixels with 3 color channels (RGB) along axis 2.
  2. Step 2: Average color channels per pixel

    Using img.mean(axis=2) calculates the average of the 3 channels for each pixel, producing a 2D grayscale image.
  3. Final Answer:

    gray_img = img.mean(axis=2) -> Option B
  4. Quick Check:

    Mean over axis 2 averages color channels [OK]
Hint: Use mean(axis=2) to average RGB channels [OK]
Common Mistakes:
  • Summing over wrong axis
  • Selecting only one color channel
  • Using flip instead of averaging
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard