File: anchors.py

package info (click to toggle)
pillow 12.0.0-1
  • links: PTS
  • area: main
  • in suites: forky
  • size: 72,636 kB
  • sloc: python: 49,518; ansic: 38,787; makefile: 302; sh: 168; javascript: 85
file content (30 lines) | stat: -rw-r--r-- 998 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from __future__ import annotations

from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("Tests/fonts/NotoSans-Regular.ttf", 16)


def test(anchor: str) -> Image.Image:
    im = Image.new("RGBA", (200, 100), "white")
    d = ImageDraw.Draw(im)
    d.line(((100, 0), (100, 100)), "gray")
    d.line(((0, 50), (200, 50)), "gray")
    d.text((100, 50), f"{anchor} example", "black", font, anchor)
    return im


if __name__ == "__main__":
    im = Image.new("RGBA", (600, 300), "white")
    d = ImageDraw.Draw(im)
    for y, row in enumerate(
        (("ma", "mt", "mm"), ("ms", "mb", "md"), ("ls", "ms", "rs"))
    ):
        for x, anchor in enumerate(row):
            im.paste(test(anchor), (x * 200, y * 100))
            if x != 0:
                d.line(((x * 200, y * 100), (x * 200, (y + 1) * 100)), "black", 3)
            if y != 0:
                d.line(((x * 200, y * 100), ((x + 1) * 200, y * 100)), "black", 3)
    im.save("docs/example/anchors.webp")
    im.show()