Pillow Resize

Overview

The Pillow Resize sample demonstrates high-quality image resizing using CV-CUDA’s GPU-accelerated Pillow-style resize operator. Unlike a plain bilinear or nearest-neighbour resize, pillowresize matches the resampling quality of Python’s Pillow library by supporting filters such as LANCZOS, HAMMING, and BOX that are especially well-suited for downscaling images.

Usage

Basic Usage

Resize an image to 224×224 (default) using the LANCZOS filter:

python3 pillowresize.py -i input.jpg

Custom Dimensions

Specify target width and height:

python3 pillowresize.py -i input.jpg -o cat_pillowresize.jpg --width 512 --height 512

Command-Line Arguments

Argument

Short Form

Default

Description

--input

-i

tabby_tiger_cat.jpg

Input image file path

--output

-o

cvcuda/.cache/cat_pillowresize.jpg

Output image file path

--width

224

Target width in pixels

--height

224

Target height in pixels

Implementation

Pillow-Quality Resize

# Pillow-style resize uses high-quality downsampling filters (e.g. LANCZOS)
# that match PIL/Pillow output more closely than a plain bilinear resize.
# The output shape must be (H, W, C) for HWC tensors.
output_image: cvcuda.Tensor = cvcuda.pillowresize(
    input_image,
    (args.height, args.width, input_image.shape[2]),
    cvcuda.Format.RGB8,
    cvcuda.Interp.LANCZOS,
)
write_image(output_image, args.output)

Key points:

  1. Output Shape: Must include channel count explicitly, e.g. (H, W, C) for HWC tensors.

  2. Format Parameter: Tells the operator how to interpret channel ordering (e.g. cvcuda.Format.RGB8).

  3. LANCZOS Filter: Produces sharper edges than LINEAR and is the recommended choice for downscaling, matching Pillow’s high-quality mode.

  4. uint8 Output: The operator preserves the input dtype; reading a JPEG returns uint8, so the result is directly viewable without rescaling.

  5. Interp Variants: HAMMING and BOX are also available and offer different quality/speed trade-offs for downscaling.

Expected Output

The output shows the image resized to the target dimensions (default 224×224) with Pillow-quality LANCZOS interpolation:

../../_images/tabby_tiger_cat.jpg

Original Input Image

../../_images/cat_pillowresize.jpg

Output: Pillow Resize to 224×224 (LANCZOS)

CV-CUDA Operators Used

Operator

Purpose

cvcuda.pillowresize()

Resize images to target dimensions using Pillow-compatible high-quality filters

Common Utilities Used

See Also