Resize Image | OpenCV vs Pillow | Python

In this part, I will show you the simple way to resize images (without keeping the aspect ratio) in OpenCV (cv2) and Pillow (PIL). If you want to resize an image while keeping the aspect ratio, read this tutorial.

Resize Image

# Pillow
pil_img_resized = pil_img.resize((NEW_WIDTH, NEW_HEIGHT))

# OpenCV
cv2_img_resized = cv2.resize(cv2_img, (NEW_WIDTH, NEW_HEIGHT))

Full Example

Pillow

from PIL import Image

# read image
pil_img = Image.open("test_images/test1.jpg")

# resize image
new_width = 512
new_height = 256
pil_img_resized = pil_img.resize((new_width, new_height))

# print the old and new size
print(f"old size: {pil_img.size}")
print(f"new size: {pil_img_resized.size}")
# show the resized image
pil_img_resized.show("pil resized image")

OpenCV

import cv2

# read image
cv2_img = cv2.imread("test_images/test1.jpg")

# resize image
new_width = 512
new_height = 256
cv2_img_resized = cv2.resize(cv2_img, (new_width, new_height))

# print the old and new shape
print(f"old shape: {cv2_img.shape}")
print(f"new shape: {cv2_img_resized.shape}")
# show the resized image
cv2.imshow("cv2 resized image", cv2_img_resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Syntax

Pillow

Image.resize(size, resample=None, box=None, reducing_gap=None)

Parameters:

  • size: The requested size in pixels, as a 2-tuple: (width, height).
  • resample: An optional resampling filter. This can be one of Resampling.NEARESTResampling.BOXResampling.BILINEARResampling.HAMMINGResampling.BICUBIC or Resampling.LANCZOS. If the image has mode “1” or “P”, it is always set to Resampling.NEAREST. If the image mode specifies the number of bits, such as “I;16”, then the default filter is Resampling.NEAREST. Otherwise, the default filter is Resampling.BICUBIC. See Filters.
  • box: An optional 4-tuple of floats providing the source image region to be scaled. The values must be within the (0, 0, width, height) rectangle. If omitted or None, the entire source is used.
  • reducing_gap: Apply optimization by resizing the image in two steps. First, reducing the image by integer times using reduce(). Second, resizing using regular resampling. The last step changes size no less than by reducing_gap times. reducing_gap may be None (no first step is performed) or should be greater than 1.0. The bigger reducing_gap, the closer the result to the fair resampling. The smaller reducing_gap, the faster resizing. With reducing_gap greater or equal to 3.0, the result is indistinguishable from fair resampling in most cases. The default value is None (no optimization).

Returns:

OpenCV

cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])

Parameters:

  • src: input image.
  • dst: output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src.
  • dsize: output image size; if it equals zero (None in Python), it is computed as:
    dsize = Size(round(fx*src.cols), round(fy*src.rows))
    Either dsize or both fx and fy must be non-zero.
  • fx: scale factor along the horizontal axis; when it equals 0, it is computed as:
    (double)dsize.width/src.cols
  • fy: scale factor along the vertical axis; when it equals 0, it is computed as:
    (double)dsize.height/src.rows
  • interpolation: interpolation method, see InterpolationFlags.

Returns:

  • An image (Numpy array).

References

Avatar photo
Steins

Developer & AI Researcher. Write about AI, web dev/hack.