Skip to main content

Quick start

Open a picture, make a thumbnail, write it. Everything else in libstb follows the same shape: a function takes an image and returns a new one, and the image you passed in is not changed.

import os

import libstb as stb

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

img = stb.Image.open("astronaut.png")
print(img.width, img.height, img.channels)

# The thumbnail fits inside the box and keeps the proportions.
thumb = img.thumbnail(128, 128)
print(thumb.width, thumb.height)

thumb.write_jpg("out/thumbnail.jpg", quality=85)
Output
512 512 3
128 128
out/thumbnail.jpg
out/thumbnail.jpg

Image.open finds the format from the bytes of the file, not from its name. thumbnail never enlarges: a picture that already fits in the box comes back at its own size. write_jpg takes a quality from 1 to 100.

Where next​

  • Open and inspect shows how to read the header without decoding, and how to ask for another channel count.
  • Resize explains the filters.
  • Canvas basics draws text and shapes on a picture.