okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-3v (aec86299d) into f-own-int: our own Qwen vision tower (mtmd stays the default)0479b72e9

main
4.2 KiB92 linesraw
1#!/usr/bin/env python3
2"""Makes the picture test set of the own vision tower (Pillow only, deterministic).
3
4    python tools/vision/own/make_test_images.py <out dir>
5
6The three pictures of tools/qwen35/pictures are copied as they are; the rest is drawn: small, odd aspect ratios, sizes that are not multiples of
732, a very large one (the resize path), the same picture as JPEG / BMP / GIF, grayscale, RGBA and palette PNGs, and a photo-like one (gradients +
8noise).  Nothing here is large enough to matter in the repo, and nothing is committed: the folder is made when a check runs.
9"""
10import os
11import shutil
12import sys
13
14from PIL import Image, ImageDraw, ImageFilter, ImageFont
15
16HERE = os.path.dirname(os.path.abspath(__file__))
17PICS = os.path.join(HERE, "..", "..", "qwen35", "pictures")
18
19
20def font(size):
21    for name in ("DejaVuSans-Bold.ttf", "arialbd.ttf", "Arial Bold.ttf", "LiberationSans-Bold.ttf"):
22        try:
23            return ImageFont.truetype(name, size)
24        except OSError:
25            pass
26    return ImageFont.load_default()
27
28
29def scene(w, h, text, seed=0):
30    """A canvas with a few coloured shapes and a line of text, scaled to the size (so small and large pictures show the same things)."""
31    im = Image.new("RGB", (w, h), (245, 245, 235))
32    d = ImageDraw.Draw(im)
33    m = min(w, h)
34    cols = [(220, 40, 40), (40, 90, 220), (30, 160, 70), (240, 190, 20)]
35    for i, c in enumerate(cols):
36        x0 = int(w * (0.08 + 0.22 * i))
37        y0 = int(h * (0.15 + 0.1 * (i % 2)))
38        r = max(2, int(m * 0.16))
39        if i % 2 == 0:
40            d.ellipse([x0, y0, x0 + r, y0 + r], fill=c)
41        else:
42            d.rectangle([x0, y0, x0 + r, y0 + r], fill=c)
43    d.text((int(w * 0.06), int(h * 0.62)), text, fill=(20, 20, 20), font=font(max(8, int(m * 0.18))))
44    return im
45
46
47def photo(w, h):
48    """Gradients plus noise, blurred a little: a photo-like picture with no flat areas."""
49    base = Image.merge("RGB", [Image.linear_gradient("L").resize((w, h)),
50                               Image.linear_gradient("L").rotate(90).resize((w, h)),
51                               Image.linear_gradient("L").rotate(45, expand=False).resize((w, h))])
52    noise = Image.effect_noise((w, h), 40).convert("RGB")
53    out = Image.blend(base, noise, 0.25).filter(ImageFilter.GaussianBlur(1.2))
54    d = ImageDraw.Draw(out)
55    d.ellipse([w * 0.3, h * 0.3, w * 0.7, h * 0.7], outline=(255, 255, 255), width=max(2, w // 100))
56    return out
57
58
59def main():
60    if len(sys.argv) != 2:
61        sys.exit(__doc__)
62    out = sys.argv[1]
63    os.makedirs(out, exist_ok=True)
64    for n in ("shapes.png", "code4217.png", "big1024.png"):
65        shutil.copyfile(os.path.join(PICS, n), os.path.join(out, n))
66    shapes = Image.open(os.path.join(PICS, "shapes.png")).convert("RGB")
67    save = lambda im, n, **kw: im.save(os.path.join(out, n), **kw)  # noqa: E731
68    save(scene(64, 48, "Hi"), "tiny_64x48.png")
69    save(scene(90, 1000, "TALL"), "tall_90x1000.png")
70    save(scene(1000, 90, "WIDE strip"), "wide_1000x90.png")
71    save(scene(33, 517, "odd"), "odd_33x517.png")
72    save(scene(999, 777, "999 x 777"), "odd_999x777.png")
73    save(scene(333, 333, "333"), "odd_333x333.png")
74    save(photo(1600, 1200), "photo_1600x1200.jpg", quality=90)
75    save(scene(4000, 3000, "HUGE 4000x3000"), "huge_4000x3000.png")
76    save(scene(8000, 500, "HUGE 8000x500"), "huge_8000x500.png")
77    # pictures for the largest image-token limit the app allows (settings: vision_tokens up to 16,384 -> 65,536 patches); not part of the usual gate list
78    save(scene(4096, 4096, "LIMIT 4096"), "limit_4096x4096.png")       # exactly 16,384 tokens at that limit
79    save(scene(7000, 5000, "LIMIT 7000x5000"), "limit_7000x5000.png")  # more than 16,384 tokens: resized down to the limit
80    save(shapes, "shapes.jpg", quality=90)
81    save(shapes, "shapes.bmp")
82    save(shapes.convert("P", palette=Image.ADAPTIVE, colors=64), "shapes_p.gif")
83    save(shapes.convert("L"), "shapes_gray.png")
84    rgba = shapes.convert("RGBA")
85    rgba.putalpha(Image.linear_gradient("L").resize(shapes.size))
86    save(rgba, "shapes_rgba.png")
87    save(shapes.convert("P", palette=Image.ADAPTIVE, colors=32), "shapes_pal.png")
88    print("wrote %d pictures to %s" % (len(os.listdir(out)), out))
89
90
91if __name__ == "__main__":
92    main()