0
0
SciPydata~20 mins

Image rotation and zoom in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Image Rotation and Zoom Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of rotated image shape
What is the shape of the image after rotating a 100x100 array by 45 degrees using scipy.ndimage.rotate with reshape=True?
SciPy
import numpy as np
from scipy.ndimage import rotate

image = np.ones((100, 100))
rotated = rotate(image, 45, reshape=True)
print(rotated.shape)
A(142, 142)
B(100, 100)
C(120, 120)
D(141, 141)
Attempts:
2 left
💡 Hint
Think about how rotating a square by 45 degrees increases the bounding box size.
data_output
intermediate
2:00remaining
Zoomed image pixel value at center
Given a 5x5 image with all zeros except the center pixel set to 1, what is the pixel value at the center after zooming by 2 using scipy.ndimage.zoom?
SciPy
import numpy as np
from scipy.ndimage import zoom

image = np.zeros((5,5))
image[2,2] = 1
zoomed = zoom(image, 2)
center_value = zoomed[5,5]
print(round(center_value, 2))
A1.0
B0.5
C0.25
D0.0
Attempts:
2 left
💡 Hint
Zooming uses interpolation which spreads the pixel value.
🔧 Debug
advanced
2:00remaining
Identify error in rotation code
What error does the following code raise? import numpy as np from scipy.ndimage import rotate image = np.ones((50, 50)) rotated = rotate(image, '90') print(rotated.shape)
SciPy
import numpy as np
from scipy.ndimage import rotate
image = np.ones((50, 50))
rotated = rotate(image, '90')
print(rotated.shape)
ATypeError: an integer is required
BValueError: invalid literal for int() with base 10: '90'
CTypeError: unsupported operand type(s) for -: 'str' and 'int'
DNo error, prints (50, 50)
Attempts:
2 left
💡 Hint
Check the type of the angle argument passed to rotate.
visualization
advanced
2:00remaining
Effect of zoom factor on image size
If you zoom a 64x64 image by a factor of 1.5 using scipy.ndimage.zoom, what will be the shape of the resulting image?
SciPy
import numpy as np
from scipy.ndimage import zoom
image = np.zeros((64,64))
zoomed = zoom(image, 1.5)
print(zoomed.shape)
A(96, 96)
B(95, 95)
C(96, 95)
D(95, 96)
Attempts:
2 left
💡 Hint
Zoom rounds the output shape to the nearest integer.
🚀 Application
expert
3:00remaining
Combining rotation and zoom effects
You have a 128x128 image. You first rotate it by 30 degrees with reshape=False, then zoom it by 0.5. What is the shape of the final image?
SciPy
import numpy as np
from scipy.ndimage import rotate, zoom
image = np.ones((128,128))
rotated = rotate(image, 30, reshape=False)
final = zoom(rotated, 0.5)
print(final.shape)
A(63, 63)
B(128, 128)
C(65, 65)
D(64, 64)
Attempts:
2 left
💡 Hint
reshape=False keeps the shape same after rotation; zoom scales down by half rounding to nearest integer.