We rotate and zoom images to change their angle or size. This helps us see images from different views or focus on details.
Image rotation and zoom in SciPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
SciPy
from scipy.ndimage import rotate, zoom rotated_image = rotate(image, angle, reshape=True) zoomed_image = zoom(image, zoom_factor)
image is a NumPy array representing the image.
angle is in degrees. Positive values rotate counter-clockwise.
Examples
SciPy
rotated = rotate(image, 45) zoomed = zoom(image, 2)
SciPy
rotated = rotate(image, -90, reshape=False) zoomed = zoom(image, 0.5)
Sample Program
This code creates a simple black and white image, rotates it 45 degrees, and zooms it by 2 times. Then it shows all three images side by side.
SciPy
import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import rotate, zoom # Create a simple 5x5 image with a white square in the center image = np.zeros((5,5)) image[1:4, 1:4] = 1 # Rotate the image 45 degrees rotated_image = rotate(image, 45, reshape=True) # Zoom the image by 2 times zoomed_image = zoom(image, 2) # Show original, rotated, and zoomed images fig, axs = plt.subplots(1, 3, figsize=(9,3)) axs[0].imshow(image, cmap='gray') axs[0].set_title('Original') axs[0].axis('off') axs[1].imshow(rotated_image, cmap='gray') axs[1].set_title('Rotated 45°') axs[1].axis('off') axs[2].imshow(zoomed_image, cmap='gray') axs[2].set_title('Zoomed x2') axs[2].axis('off') plt.tight_layout() plt.show()
Important Notes
Rotation can change the image size if reshape=True. Set it to False to keep original size but parts may be cut off.
Zooming can make images bigger or smaller. Zoom factors >1 enlarge, <1 shrink.
Images are NumPy arrays, so you can use other NumPy tools to prepare or analyze them.
Summary
Use scipy.ndimage.rotate to turn images by any angle.
Use scipy.ndimage.zoom to resize images smoothly.
These tools help change how images look for analysis or display.
Practice
1. What does the function
scipy.ndimage.rotate do to an image?easy
Solution
Step 1: Understand the function purpose
scipy.ndimage.rotateis designed to rotate images by a given angle in degrees.Step 2: Compare options with function behavior
Only turning or rotating the image matches the function's purpose; other options describe different image operations.Final Answer:
It turns the image by a specified angle. -> Option BQuick 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
Solution
Step 1: Check function signatures
scipy.ndimage.rotatetakes the image first, then the angle as the second argument.Step 2: Validate correct argument order
scipy.ndimage.rotate(img, 45)correctly usesrotate(img, 45). Options C and D swap arguments incorrectly, and B uses zoom instead of rotate.Final Answer:
scipy.ndimage.rotate(img, 45) -> Option AQuick 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
Solution
Step 1: Understand zoom factor effect
A zoom factor of 2 doubles the size of each dimension of the image.Step 2: Calculate new shape
Original shape is (100, 100). Doubling each dimension gives (200, 200).Final Answer:
(200, 200) -> Option CQuick 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
Solution
Step 1: Check argument order for rotate()
scipy.ndimage.rotateexpects the image array as the first argument, then the angle.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.Final Answer:
The image should be the first argument. -> Option AQuick 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
Solution
Step 1: Understand operation order
First rotate by 90 degrees, then zoom by 0.5 to reduce size by half.Step 2: Check code correctness
rotated = scipy.ndimage.rotate(img, 90) zoomed = scipy.ndimage.zoom(rotated, 0.5)
correctly rotatesimgby 90, then zooms the rotated image by 0.5. Other options mix argument order or use wrong values.Final Answer:
rotated = scipy.ndimage.rotate(img, 90) zoomed = scipy.ndimage.zoom(rotated, 0.5) -> Option DQuick 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
