Skip to main content

Crop, flip, rotate and pad

Every geometry call returns a new image and leaves the one you called it on alone. The sheet below is made by the example: the picture, mirrored, upside down, turned a quarter, transposed, and framed, in a row.

import os

import libstb as stb

os.makedirs("out", exist_ok=True)

# A cat, so every flip and turn is easy to see.
img = stb.Image.open("chelsea.png").thumbnail(240, 160)
print("source:", img.width, "x", img.height)

# The rectangle (x, y, width, height) has to lie inside the image.
face = img.crop(80, 20, 100, 100)
print("crop:", face.width, "x", face.height)

mirrored = img.flip_horizontal()
upside_down = img.flip_vertical()
turned = img.rotate90() # one quarter turn counter-clockwise
print("rotate90:", turned.width, "x", turned.height)
transposed = img.transpose() # swap rows and columns

# A border of the given widths: left, top, right, bottom.
framed = img.pad(10, 10, 10, 10, fill=(255, 255, 255))
print("pad:", framed.width, "x", framed.height)

# A thumbnail fits in the box and never enlarges.
small = img.thumbnail(64, 64)
print("thumbnail:", small.width, "x", small.height)
print("already small enough:", img.thumbnail(1000, 1000).width, "x", img.thumbnail(1000, 1000).height)

# Nothing above changed the source.
print("source:", img.width, "x", img.height)

sheet = stb.Image.hstack([img, mirrored, upside_down, turned, transposed, framed], gap=8, background="white", align="center")
sheet.write("out/geometry.png")
face.write("out/face.png")
Output
source: 240 x 160
crop: 100 x 100
rotate90: 160 x 240
pad: 260 x 180
thumbnail: 64 x 43
already small enough: 240 x 160
source: 240 x 160
out/geometry.png
out/geometry.png

What each call does​

  • crop(x, y, width, height) takes a rectangle. It has to lie inside the image, or the call raises an error.
  • flip_horizontal / flip_vertical mirror left to right or top to bottom.
  • rotate90(turns) turns by quarter turns, counter-clockwise for a positive number and clockwise for a negative one. Its default is one turn. A 240 by 160 picture comes back 160 by 240.
  • transpose swaps rows and columns: a mirror across the diagonal.
  • pad(left, top, right, bottom, fill) adds a border. fill is one value for every channel or one value per channel.
  • thumbnail(max_width, max_height) fits the picture inside the box and keeps the proportions. It never enlarges, so a picture that already fits is returned at its own size. It uses the same filters as resize.

Putting pictures together​

hstack and vstack place several images side by side or one above the other, gap pixels apart, on a background, with the shorter ones aligned at the start, the middle or the end. That is how the sheet above was made.

API at a glance​

Cropimg.crop(x, y, w, h)
Mirrorimg.flip_horizontal(), img.flip_vertical()
Turnimg.rotate90(turns=1)
Transposeimg.transpose()
Borderimg.pad(l, t, r, b, fill=...)
Fit in a boximg.thumbnail(w, h)
Side by sidestb.Image.hstack(images, gap=, background=, align=)
One above the otherstb.Image.vstack(...)