Challenge - 5 Problems
Image Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this image plot code?
Consider the following Python code using matplotlib to display an image. What will be the shape of the displayed image array?
Matplotlib
import matplotlib.pyplot as plt import numpy as np image = np.random.rand(100, 150) plt.imshow(image, cmap='gray') plt.show() print(image.shape)
Attempts:
2 left
💡 Hint
Remember that numpy arrays for grayscale images have 2 dimensions: height and width.
✗ Incorrect
The image array is created with shape (100, 150), meaning 100 rows (height) and 150 columns (width). This matches the shape printed and the image displayed.
❓ data_output
intermediate1:30remaining
What is the pixel value at position (50, 75)?
Given this code that creates a grayscale image with a gradient, what is the pixel value at row 50, column 75?
Matplotlib
import numpy as np image = np.tile(np.linspace(0, 1, 150), (100, 1)) print(round(image[50, 75], 2))
Attempts:
2 left
💡 Hint
The image is a horizontal gradient from 0 to 1 over 150 columns.
✗ Incorrect
The pixel value at column 75 is about halfway through the gradient, so approximately 0.5.
❓ visualization
advanced2:00remaining
Which plot shows the correct color mapping for this image?
You have an image array with values from 0 to 255. Which matplotlib code snippet correctly displays it as a grayscale image?
Matplotlib
import matplotlib.pyplot as plt import numpy as np image = np.random.randint(0, 256, (50, 50))
Attempts:
2 left
💡 Hint
Grayscale images need the 'gray' colormap to show shades of gray.
✗ Incorrect
Option C uses cmap='gray' which maps pixel values to grayscale colors correctly. Other colormaps show colors, not grayscale.
🔧 Debug
advanced2:30remaining
Why does this image display incorrectly?
This code tries to display a color image but the colors look wrong. What is the cause?
Matplotlib
import matplotlib.pyplot as plt import numpy as np image = np.random.randint(0, 256, (100, 100, 3)) plt.imshow(image) plt.show()
Attempts:
2 left
💡 Hint
Matplotlib expects RGB order for color images.
✗ Incorrect
If the image was loaded from OpenCV, it might be in BGR order, causing wrong colors in matplotlib which expects RGB.
🚀 Application
expert3:00remaining
How to efficiently resize a large image array for faster plotting?
You have a large image array of shape (4000, 6000). You want to display a smaller version quickly using matplotlib. Which approach is best?
Attempts:
2 left
💡 Hint
Reducing image size by sampling pixels speeds up plotting.
✗ Incorrect
Option A downsamples the image by taking every 10th pixel, reducing size and speeding up display. Option A is invalid because numpy arrays don't have resize method like that.