Skip to main content

The Resizer

Resize takes a filter by name. When you need more than that, the Resizer holds every setting of a resize in one object you build once: the filter, the edge, whether to blend in linear light, and a limit on the memory of the result. It is what resize uses when you give it nothing, and you can pass the same one to as many calls as you like.

import os

import libstb as stb

os.makedirs("out", exist_ok=True)
img = stb.Image.open("astronaut.png")

# A Resizer holds the settings of a resize: the filter, the edge, whether to blend in linear light, a size limit.
# resize(w, h) with no Resizer uses stb.Resizer(): the automatic filter, clamp, linear light.
linear = img.resize(64, 64, stb.Resizer("mitchell"))
plain = img.resize(64, 64, stb.Resizer("mitchell", srgb=False))
linear.write("out/linear_light.png")
plain.write("out/plain_srgb.png")
print(f"linear light against plain sRGB: mean absolute difference {stb.mae(linear, plain):.2f}")

# The edge says what lies outside the picture, which only matters near the border.
small = img.thumbnail(32, 32)
clamped = small.resize(256, 256, stb.Resizer("cubic", edge=stb.Resizer.Edge.CLAMP))
for name in ("clamp", "reflect", "wrap", "zero"):
result = small.resize(256, 256, stb.Resizer("cubic", edge=getattr(stb.Resizer.Edge, name.upper())))
result.write(f"out/edge_{name}.png")
print(f"edge {name} against clamp: mean absolute difference {stb.mae(result, clamped):.2f}")

# One Resizer can be used again and again.
resizer = stb.Resizer("bspline")
sizes = [img.resize(w, w, resizer).width for w in (48, 96, 192)]
print("one resizer, three sizes:", sizes)

# A limit on the memory of the result, checked before anything is allocated.
try:
img.resize(20000, 20000, stb.Resizer(max_bytes=1 << 20))
except stb.LimitError:
print("20000 x 20000 over a 1 MiB limit: limit error")
Output
linear light against plain sRGB: mean absolute difference 5.06
edge clamp against clamp: mean absolute difference 0.00
edge reflect against clamp: mean absolute difference 0.25
edge wrap against clamp: mean absolute difference 1.93
edge zero against clamp: mean absolute difference 1.06
one resizer, three sizes: [48, 96, 192]
20000 x 20000 over a 1 MiB limit: limit error
linear light (the default)
linear light (the default)
plain sRGB
plain sRGB
edge clamp
edge clamp
edge reflect
edge reflect
edge wrap
edge wrap
edge zero
edge zero

Linear light​

Pixel values in a photograph are not proportional to light: they are stored with a curve (sRGB). Averaging them directly, as a resize does, makes bright and dark areas that are next to each other come out too dark. By default libstb converts to linear light, resizes, and converts back, which is the physically right way, and a little slower. The alpha channel is always blended linearly. srgb=False turns the conversion off. It is the setting to use when you compare with a library that does not do it, and for pictures that are not photographs (a mask, a normal map, a lookup table) where the numbers are data and not light.

The edge​

A filter reaches past the border of the picture, to pixels that do not exist, and edge decides what it finds there: clamp repeats the nearest edge pixel (the default), reflect mirrors the picture, wrap takes it from the opposite side, as for a tile that repeats, and zero reads black. The difference is a few pixels wide at the border, so the example enlarges a tiny picture to make it visible, and prints how far each mode is from clamp.

The limit​

max_bytes bounds the size of the result in bytes, 512 MiB by default (a result over 2 GiB is refused whatever the limit). A request for a 20 000 by 20 000 picture over a limit of 1 MiB is a limit error. It is the same rule as for untrusted input, and it matters when the size comes from a user.

API at a glance​

Build onestb.Resizer(filter, edge=, srgb=, max_bytes=)
The filtera name or a stb.Resizer.Filter
The edgestb.Resizer.Edge.CLAMP, REFLECT, WRAP, ZERO
Use itimg.resize(w, h, resizer)