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 |
|---|---|---|---|
|
|
tabby_tiger_cat.jpg |
Input image file path |
|
|
cvcuda/.cache/cat_pillowresize.jpg |
Output image file path |
|
224 |
Target width in pixels |
|
|
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:
Output Shape: Must include channel count explicitly, e.g.
(H, W, C)for HWC tensors.Format Parameter: Tells the operator how to interpret channel ordering (e.g.
cvcuda.Format.RGB8).LANCZOS Filter: Produces sharper edges than LINEAR and is the recommended choice for downscaling, matching Pillow’s high-quality mode.
uint8 Output: The operator preserves the input dtype; reading a JPEG returns
uint8, so the result is directly viewable without rescaling.Interp Variants:
HAMMINGandBOXare 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:
Original Input Image |
Output: Pillow Resize to 224×224 (LANCZOS) |
CV-CUDA Operators Used
Operator |
Purpose |
|---|---|
Resize images to target dimensions using Pillow-compatible high-quality filters |
Common Utilities Used
read_image() - Load image as CV-CUDA tensor
write_image() - Save resized image
See Also
Resize Operator - Standard GPU resize operator
Common Utilities - Helper functions