Sub topic 5
Resizing to a Size or by a Scale Factor
This chapter assumes that you know basic Python, NumPy array indexing, and the difference between an image’s width and height. OpenCV stores an image as a NumPy array. Its shape is usually (height, width, channels), while resizing functions use sizes as (width, height).
Choosing an Exact Size
Use cv2.resize when the output must have a specific width and height. The dsize argument is a tuple in (width, height) order.
import cv2
img = cv2.imread("photo.jpg")
if img is None:
raise FileNotFoundError("Could not read photo.jpg")
# dsize is (width, height)
small = cv2.resize(
img,
(640, 480),
interpolation=cv2.INTER_AREA,
)
print("Original:", img.shape)
print("Resized:", small.shape)
cv2.imwrite("photo_640x480.jpg", small)If the original image does not have a 4:3 aspect ratio, forcing it to (640, 480) will distort the image. Circles may look like ovals, and people or objects may appear stretched.
Scaling by a Factor
Instead of specifying the final size, provide scale factors with fx and fy. A factor of 0.5 halves each dimension. A factor of 2.0 doubles each dimension.
import cv2
img = cv2.imread("photo.jpg")
if img is None:
raise FileNotFoundError("Could not read photo.jpg")
half = cv2.resize(
img,
None,
fx=0.5,
fy=0.5,
interpolation=cv2.INTER_AREA,
)
double = cv2.resize(
img,
None,
fx=2.0,
fy=2.0,
interpolation=cv2.INTER_CUBIC,
)
cv2.imwrite("photo_half.jpg", half)
cv2.imwrite("photo_double.jpg", double)When using scale factors, pass None for dsize. OpenCV calculates the new dimensions from the original dimensions and the factors. Rounding can occur when the result is not a whole number of pixels.
Choosing Interpolation
Interpolation estimates pixel values between existing pixels. The method affects sharpness, detail, and processing time.
| Operation | Common choice | Reason |
|---|---|---|
| Shrinking | `cv2.INTER_AREA` | Reduces aliasing and preserves area information |
| Enlarging | `cv2.INTER_LINEAR` | Fast and usually adequate |
| Enlarging for smoother results | `cv2.INTER_CUBIC` | Uses more neighboring pixels and can look smoother |
Interpolation cannot recreate detail that was never captured. Enlarging a small image produces more pixels, but it does not recover missing information.
Preserving the Aspect Ratio
To resize an image to a target width without distortion, calculate the new height using the original ratio.
import cv2
def resize_to_width(image, width, interpolation=cv2.INTER_AREA):
height, original_width = image.shape[:2]
scale = width / original_width
new_height = round(height * 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")
thumbnail = resize_to_width(img, 320)
cv2.imwrite("thumbnail.jpg", thumbnail)The same idea works when targeting a height. Calculate the width from the height instead. Keeping both scale factors equal, such as fx=0.5 and fy=0.5, also preserves the aspect ratio.
Common Errors
- Confusing
(height, width)fromimage.shapewith(width, height)required bycv2.resize. - Using different
fxandfyvalues when you want to preserve proportions. - Enlarging an image and expecting new fine detail.
- Choosing
INTER_CUBICeverywhere without considering its extra processing cost. - Forgetting to check whether
cv2.imreadreturnedNone.
Exercise
An image has shape (1200, 800, 3). What dsize should you use to shrink it to half its original dimensions without changing its aspect ratio?
Show answer
Use (400, 600), because dsize is (width, height), while the original width and height are 800 and 1200.