Sub topic 7
Resizing Images Without Distortion
This chapter assumes basic Python, NumPy array indexing, and OpenCV installation. You should also know that an image array uses the order [height, width, channels], while OpenCV often receives sizes as (width, height).
Why aspect ratio matters
An image’s aspect ratio is the relationship between its width and height:
\text{aspect ratio} = \frac{\text{width}}{\text{height}}For example, an image that is 1200 pixels wide and 800 pixels high has an aspect ratio of 1.5. If you resize it to 600 by 400, the ratio stays 1.5, so the image keeps its original proportions.
Distortion happens when width and height use different scale factors. A circle may become an ellipse, and people or objects may look stretched.
Calculate one dimension from the other
To resize an image to a target width, calculate the height with the same scale factor:
\text{scale} = \frac{\text{target width}}{\text{original width}}\text{new height} = \text{original height} \times \text{scale}The round function converts the calculated height to an integer, because pixel dimensions must be whole numbers.
import cv2
def resize_to_width(image, width, interpolation=cv2.INTER_AREA):
h, w = image.shape[:2]
if w <= 0 or width <= 0:
raise ValueError("Image width and target width must be positive")
scale = width / w
new_height = round(h * scale)
return cv2.resize(
image,
(width, new_height),
interpolation=interpolation,
)
img = cv2.imread("photo.jpg")
if img is None:
raise FileNotFoundError("Could not read photo.jpg")
thumb = resize_to_width(img, 320)
cv2.imwrite("thumbnail.jpg", thumb)This helper works whether the target width is smaller or larger than the original. If the source is 1200 by 800 and the target width is 320, the new height is about 213 pixels. Both dimensions use the same scale factor.
You can use the same idea when a target height is fixed:
\text{new width} =
\text{original width}
\times
\frac{\text{target height}}{\text{original height}}Choosing the interpolation method
Interpolation estimates pixel values between existing pixels. OpenCV uses it when it must create a new image grid.
| Operation | Common choice | Reason |
|---|---|---|
| Shrinking an image | `cv2.INTER_AREA` | Reduces aliasing and preserves area information |
| Enlarging an image | `cv2.INTER_LINEAR` | Fast and usually smooth |
| Enlarging for higher smoothness | `cv2.INTER_CUBIC` | Smoother, but slower |
Aliasing is unwanted jaggedness or repeated patterns that can appear when an image is reduced. INTER_AREA is a useful default for thumbnails and other downscaled images. It does not restore detail that was absent from the original.
Check the result
After resizing, inspect the shape rather than relying on a filename or visual guess.
print("Original:", img.shape[:2])
print("Thumbnail:", thumb.shape[:2])For images with an odd original dimension, rounding can make the result differ by one pixel from a mathematically exact value. This is normal. The important property is that both dimensions came from the same scale factor.
Exercise
An image is 1600 pixels wide and 1000 pixels high. What height should it have when resized to a width of 640 pixels?
Show answer
The scale factor is 640 / 1600 = 0.4. The new height is 1000 × 0.4 = 400 pixels, so the result is 640 × 400.