Complete the code to create a 3x3 grayscale image filled with zeros.
import numpy as np image = np.[1]((3, 3))
Using np.zeros creates an array filled with zeros, which represents a black image in grayscale.
Complete the code to set the pixel at row 1, column 2 to white (255) in a grayscale image.
image = np.zeros((3, 3), dtype=np.uint8) image[1] = 255
In NumPy, the first index is the row and the second is the column. So image[1, 2] accesses row 1, column 2.
Fix the error in the code to invert a grayscale image (255 - pixel value).
inverted = 255 - image[1]
Using .copy() creates a new array so the inversion does not affect the original image.
Fill both blanks to create a dictionary with pixel positions as keys and their values for pixels greater than 100.
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}In NumPy, shape[0] is the number of rows and shape[1] is the number of columns.
Fill all three blanks to create a new image where pixels less than 50 are set to 0, others remain unchanged.
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])])
The condition keeps pixels greater or equal to 50 unchanged; pixels less than 50 are set to 0.