Bird
Raised Fist0
SciPydata~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the function scipy.ndimage.rotate do to an image?
easy
A. It adds noise to the image.
B. It turns the image by a specified angle.
C. It crops the image to a smaller size.
D. It changes the image colors.

Solution

  1. Step 1: Understand the function purpose

    scipy.ndimage.rotate is designed to rotate images by a given angle in degrees.
  2. Step 2: Compare options with function behavior

    Only turning or rotating the image matches the function's purpose; other options describe different image operations.
  3. Final Answer:

    It turns the image by a specified angle. -> Option B
  4. Quick Check:

    Rotate = Turn image [OK]
Hint: Rotate means turn image by angle [OK]
Common Mistakes:
  • Confusing rotate with crop or color change
  • Thinking rotate changes image size
  • Assuming rotate adds effects like noise
2. Which of the following is the correct way to rotate an image array img by 45 degrees using scipy?
easy
A. scipy.ndimage.rotate(img, 45)
B. scipy.ndimage.zoom(img, 45)
C. scipy.ndimage.rotate(45, img)
D. scipy.ndimage.zoom(45, img)

Solution

  1. Step 1: Check function signatures

    scipy.ndimage.rotate takes the image first, then the angle as the second argument.
  2. Step 2: Validate correct argument order

    scipy.ndimage.rotate(img, 45) correctly uses rotate(img, 45). Options C and D swap arguments incorrectly, and B uses zoom instead of rotate.
  3. Final Answer:

    scipy.ndimage.rotate(img, 45) -> Option A
  4. Quick Check:

    rotate(image, angle) correct syntax [OK]
Hint: Image first, angle second in rotate() [OK]
Common Mistakes:
  • Swapping argument order
  • Using zoom instead of rotate
  • Passing angle before image
3. What will be the shape of the output image after applying scipy.ndimage.zoom(img, 2) if img.shape is (100, 100)?
medium
A. (50, 50)
B. (100, 100)
C. (200, 200)
D. (100, 200)

Solution

  1. Step 1: Understand zoom factor effect

    A zoom factor of 2 doubles the size of each dimension of the image.
  2. Step 2: Calculate new shape

    Original shape is (100, 100). Doubling each dimension gives (200, 200).
  3. Final Answer:

    (200, 200) -> Option C
  4. Quick Check:

    Zoom 2x doubles shape [OK]
Hint: Zoom factor multiplies each dimension [OK]
Common Mistakes:
  • Confusing zoom with cropping
  • Thinking zoom keeps shape same
  • Mixing width and height dimensions
4. You run this code but get an error:
scipy.ndimage.rotate(45, img)
What is the problem?
medium
A. The image should be the first argument.
B. The angle should be the first argument.
C. The rotate function does not accept two arguments.
D. The angle must be in radians, not degrees.

Solution

  1. Step 1: Check argument order for rotate()

    scipy.ndimage.rotate expects the image array as the first argument, then the angle.
  2. Step 2: Identify error cause

    Passing angle first and image second causes a type error because the function tries to treat the number as an array.
  3. Final Answer:

    The image should be the first argument. -> Option A
  4. Quick Check:

    Image first, angle second in rotate() [OK]
Hint: Image must come before angle in rotate() [OK]
Common Mistakes:
  • Swapping argument order
  • Assuming angle must be radians
  • Thinking rotate takes only one argument
5. You want to rotate an image by 90 degrees and then zoom it to half its size. Which code sequence correctly does this?
hard
A.
rotated = scipy.ndimage.rotate(img, 0.5)
zoomed = scipy.ndimage.zoom(rotated, 90)
B.
zoomed = scipy.ndimage.zoom(img, 0.5)
rotated = scipy.ndimage.rotate(zoomed, 90)
C.
zoomed = scipy.ndimage.zoom(img, 90)
rotated = scipy.ndimage.rotate(zoomed, 0.5)
D.
rotated = scipy.ndimage.rotate(img, 90)
zoomed = scipy.ndimage.zoom(rotated, 0.5)

Solution

  1. Step 1: Understand operation order

    First rotate by 90 degrees, then zoom by 0.5 to reduce size by half.
  2. Step 2: Check code correctness

    rotated = scipy.ndimage.rotate(img, 90)
    zoomed = scipy.ndimage.zoom(rotated, 0.5)
    correctly rotates img by 90, then zooms the rotated image by 0.5. Other options mix argument order or use wrong values.
  3. Final Answer:

    rotated = scipy.ndimage.rotate(img, 90) zoomed = scipy.ndimage.zoom(rotated, 0.5) -> Option D
  4. Quick Check:

    Rotate 90°, then zoom 0.5 [OK]
Hint: Rotate first, then zoom with correct factors [OK]
Common Mistakes:
  • Swapping zoom and rotate order
  • Using zoom factor as angle
  • Passing wrong argument order