Skip to main content

Warp

A warp builds a new picture by looking each of its pixels up in the source through a transform. warp_affine takes a matrix (translate, rotate, scale, shear, which keep parallel lines parallel). warp_perspective takes a homography, a projective transform that can also make parallel lines meet, which is what a camera does to a rectangle seen at an angle.

import os

import libstb as stb

os.makedirs("out", exist_ok=True)
img = stb.Image.open("chelsea.png")
w, h = img.width, img.height

# An affine warp takes the points of the source to the points of the result. Read the chain from the bottom up:
# move the center to the origin, scale, rotate, move it back.
m = stb.Matrix().translate(w / 2, h / 2).rotate(0.3).scale(0.7, 0.7).translate(-w / 2, -h / 2)
rotated = stb.warp_affine(img, m) # outside the source is black, by default
rotated.write("out/rotated.png")
stb.warp_affine(img, m, edge="clamp").write("out/rotated_clamp.png")

# A perspective warp is given by four points and where they go.
corners = [(0, 0), (w, 0), (w, h), (0, h)]
leaning = [(60, 40), (w - 40, 0), (w - 10, h - 20), (10, h)]
homography = stb.Homography.from_quad(corners, leaning)
stb.warp_perspective(img, homography, w, h).write("out/leaning.png")
x, y = homography.map(w / 2, h / 2)
print(f"the center of the picture goes to ({round(x)}, {round(y)})")

# The other way: take the four corners of a tilted card and make them the corners of a flat picture.
card = [(70, 30), (w - 60, 10), (w - 30, h - 40), (40, h - 20)]
flat = stb.Homography.from_quad(card, [(0, 0), (240, 0), (240, 160), (0, 160)])
stb.warp_perspective(img, flat, 240, 160).write("out/straightened.png")
Output
the center of the picture goes to (222, 142)
affine, edge zero
affine, edge zero
affine, edge clamp
affine, edge clamp
perspective
perspective
a card straightened
a card straightened

The convention​

The transform takes a point of the source to a point of the result, in the canvas convention of Matrix: the center of pixel (x, y) is the point (x + 0.5, y + 0.5), so the identity returns the picture and a whole-number translation moves it by exactly that many pixels. Each call on a matrix chain applies to the point first, so read it from the bottom up, as in the example.

The result​

  • out_width and out_height are the size of the result. Zero, the default, is the size of the picture, each on its own.
  • edge says what is read where the transform reaches outside the picture. The default is zero: black, or transparent black on an RGBA picture. clamp repeats the edge pixels, as in the second picture.
  • interpolation is nearest, bilinear (the default) or bicubic. Every channel is sampled on its own, alpha included.

A homography from four points​

Homography.from_quad(src, dst) fits the transform that takes four points to four points. Give the corners of a tilted card in the photo as src and the corners of a flat rectangle as dst, and the warp straightens the card, as in the last picture. It fails for four points that are degenerate, such as three on a line. A homography has map(x, y) to follow one point, inverse() and then() to combine transforms.

API at a glance​

Affine warpstb.warp_affine(img, matrix, w, h, interpolation=, edge=)
Perspective warpstb.warp_perspective(img, homography, w, h)
A homography from pointsstb.Homography.from_quad(src, dst)
Follow a pointh.map(x, y), a tuple