Skip to main content

Resize

resize(width, height) returns a new image of the requested size. The image you call it on is not changed.

from pathlib import Path

import libstb as stb

out = Path("out")
out.mkdir(exist_ok=True)

img = stb.Image.open("astronaut.png")
print(f"{img.width}x{img.height}")

# Shrink with the default filter, then enlarge the tiny result with each filter by name.
tiny = img.resize(64, 64)
tiny.write(out / "tiny.png")
print(f"{tiny.width}x{tiny.height}")

for name in ("nearest", "linear", "cubic", "bspline", "mitchell"):
tiny.resize(256, 256, name).write(out / f"{name}.png")
Output
512x512
64x64

The code shrinks astronaut.png to 64×64 with the default filter, then enlarges that small picture to 256×256 once per filter. The small picture is the poorest possible input on purpose, so the differences between filters are easy to see.

What it produces​

These pictures are the output of the example above, not separate artwork.

64×64, default filter (shown at 3×)
64×64, default filter (shown at 3×)
nearest
nearest
linear
linear
cubic
cubic
bspline
bspline
mitchell
mitchell

Choosing the filter​

The filter is picked by name or by enum. With no filter, libstb uses Catmull-Rom when it enlarges and Mitchell when it shrinks. Names are case-insensitive, and - or a space counts as _.

FilterNames
defaultauto, default
nearestnearest, point
linearlinear, bilinear, triangle
cubic (Catmull-Rom)cubic, bicubic, catmull_rom
B-splinebspline, cubic_bspline
Mitchellmitchell
boxbox, area

There is no Lanczos filter. An unknown name raises ValueError and lists the valid names.

By default libstb blends pixels in linear light. Resizer(filter, srgb=False) turns that off, which is the like-for-like setting when comparing with libraries that do not do it.

How fast is it​

The reference in these two charts is OpenCV, the library that was fastest on the most pictures. On average libstb, with either setting, takes longer than OpenCV here, and less time than Pillow and scikit-image. The note under each chart explains why OpenCV's linear and cubic modes are not a like-for-like comparison when shrinking.

resize, half size, cubic (Catmull-Rom)

  • OpenCV1.00× (reference)
  • libstb1.81×
  • libstb (sRGB-correct)3.56×
  • Pillow7.91×
  • scikit-image352.1×

libstb, Pillow and scikit-image prefilter when shrinking; OpenCV's INTER_LINEAR and INTER_CUBIC do not (faster, and they alias), and INTER_AREA is its antialiasing counterpart. "libstb" is Resizer(filter, srgb=False), the like-for-like pairing; "libstb (sRGB-correct)" is the library's own default, which blends in linear light like a correct resizer should, listed separately so the price of that is visible. 3-channel images only. scikit-image is skipped above 1 MP (seconds per call).

resize, double size, cubic (Catmull-Rom)

  • OpenCV1.00× (reference)
  • libstb2.05×
  • libstb (sRGB-correct)3.88×
  • Pillow7.99×
  • scikit-image872.2×

libstb, Pillow and scikit-image prefilter when shrinking; OpenCV's INTER_LINEAR and INTER_CUBIC do not (faster, and they alias), and INTER_AREA is its antialiasing counterpart. "libstb" is Resizer(filter, srgb=False), the like-for-like pairing; "libstb (sRGB-correct)" is the library's own default, which blends in linear light like a correct resizer should, listed separately so the price of that is visible. 3-channel images only. scikit-image is skipped above 1 MP (seconds per call).

Time of each library divided by the time of the reference (lower is better), averaged over the pictures of the corpus (18 compared per operation). Log scale. Measured 2026-10-15 11:24 UTC, 7 repeats, Intel(R) Xeon(R) Processor, 1 logical core(s). Source: benchmarks/outputs/benchmark.json.

API at a glance​

Open a filestb.Image.open(path)
Resizeimg.resize(w, h, filter)
Writeimg.write(path)
Pick the filtera name, or a Resizer.Filter for Resizer; an unknown name raises ValueError
Linear-light blending offstb.Resizer(filter, srgb=False)