0
0
NumPydata~10 mins

Basic image manipulation with arrays in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a 3x3 grayscale image filled with zeros.

NumPy
import numpy as np
image = np.[1]((3, 3))
Drag options to blanks, or click blank then click option'
Azeros
Bfull
Cempty
Dones
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.ones creates an image filled with white pixels, not black.
Using np.empty creates an uninitialized array with random values.
2fill in blank
medium

Complete the code to set the pixel at row 1, column 2 to white (255) in a grayscale image.

NumPy
image = np.zeros((3, 3), dtype=np.uint8)
image[1] = 255
Drag options to blanks, or click blank then click option'
A(1, 2)
B[1, 2]
C[2, 1]
D(2, 1)
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping row and column indices.
Using square brackets inside the indexing which is invalid syntax.
3fill in blank
hard

Fix the error in the code to invert a grayscale image (255 - pixel value).

NumPy
inverted = 255 - image[1]
Drag options to blanks, or click blank then click option'
A[]
B()
C.astype(int)
D.copy()
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or brackets after the array name causes syntax errors.
Not copying the array modifies the original image unintentionally.
4fill in blank
hard

Fill both blanks to create a dictionary with pixel positions as keys and their values for pixels greater than 100.

NumPy
pixels = {(row, col): image[row, col] for row in range(image.shape[[1]]) for col in range(image.shape[[2]]) if image[row, col] > 100}
Drag options to blanks, or click blank then click option'
A0
B1
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the indices for rows and columns.
Using an index that is out of range for the shape tuple.
5fill in blank
hard

Fill all three blanks to create a new image where pixels less than 50 are set to 0, others remain unchanged.

NumPy
new_image = np.array([[image[row, col] if image[row, col] [1] [2] else [3] for col in range(image.shape[1])] for row in range(image.shape[0])])
Drag options to blanks, or click blank then click option'
A>=
B50
C0
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>=' reverses the condition.
Setting the else value incorrectly changes pixel values.