Canvas basics
draw_text puts one text on an image. When a picture needs a title, a caption, a watermark and a couple of shapes, a canvas is the better tool: it collects everything on one copy of the picture and draws it in the order you added it, in a single finish().
import os
import libstb as stb
os.makedirs("out", exist_ok=True)
photo = stb.Image.open("astronaut.png")
font = stb.Font.open("Vazirmatn-Regular.ttf")
# The canvas copies the picture once. The shapes and texts are collected and drawn, in order, by finish().
canvas = stb.Canvas(photo)
# Defaults for the texts added from now on.
canvas.set_font(font, 48).set_color("white")
# Shapes: smooth edges, filled with the color you give.
canvas.add_rect(10, 10, 220, 60).fill((0, 0, 0, 140)).radius(12).stroke(2, "white")
canvas.add_line(10, 78, 230, 78).stroke(3, "#ffcc00")
canvas.add_text("Title", 20, 20) # uses the defaults
# Settings for one text only.
canvas.add_text("Subtitle", 20, 80).color("#ffcc00").bg_color("#222222").padding(10)
# With the center alignment x is the middle of every line.
middle = photo.width // 2
canvas.add_text("first line\nsecond, longer line", middle, photo.height - 140).align("center")
# Translucent text on a translucent box: the alpha of a color is honored.
canvas.add_text("watermark", middle, photo.height - 50).align("center").color((255, 255, 255, 120)).bg_color((0, 0, 0, 90)).padding(6).size(24)
result = canvas.finish()
print("result:", result.width, "x", result.height)
result.write("out/canvas.png")
result: 512 x 512

How it works
- Create the canvas from an image. It copies the image once, so the original is never changed.
- Defaults set with
set_font,set_color,set_sizeandset_alignapply to the texts you add after them. - Add shapes, texts, images and outlines. Each
add_*returns the new item, and the item's own settings are calls on it that return it again, so they chain:add_text("watermark", x, y).align("center").size(24). - Finish draws everything in the order added and returns the result. After
finish()the canvas is done and takes no moreadd_*.
Texts
An item overrides the defaults for itself only: font, color, size, align, a bg_color for a box behind the text and a padding around it. The position is the top-left of the text box, and with center or right it is the middle or right edge of every line, as in render text. A color with alpha is honored, so translucent text on a translucent box works, like the watermark here.
Shapes
add_rect, add_ellipse and add_line draw with smooth edges. A rectangle can have rounded corners (radius), a fill, a stroke, or both. Shapes and colors has them one by one.
API at a glance
| Create | stb.Canvas(image) |
|---|---|
| Defaults | canvas.set_font(font, px).set_color(c) |
| A text | canvas.add_text(text, x, y).color(c).align("center") |
| A rectangle | canvas.add_rect(x, y, w, h).fill(c).radius(r).stroke(w, c) |
| An ellipse, a line | canvas.add_ellipse(x, y, w, h), canvas.add_line(x1, y1, x2, y2) |
| Draw it all | canvas.finish() |