File size: 1,944 Bytes
fd11f30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""生成两张 448x448 测试图(纯标准库,不依赖 PIL),供 VLM 带图请求使用。

- orange: 纯橙色。模型若真"看到"图,问颜色应答橙色;瞎猜则说明视觉链路没通。
- square: 白底黑方块。用于形状/内容识别的第二个判别样本。

输出 test_images.json,内容为 OpenAI image_url 用的 data URI。
"""
import base64
import json
import os
import struct
import zlib


def png_chunk(tag, data):
    return (
        struct.pack(">I", len(data))
        + tag
        + data
        + struct.pack(">I", zlib.crc32(tag + data) & 0xFFFFFFFF)
    )


def write_png(w, h, pixel):
    raw = bytearray()
    for y in range(h):
        raw.append(0)  # filter type 0 (None)
        for x in range(w):
            raw.extend(pixel(x, y))
    comp = zlib.compress(bytes(raw), 9)
    return (
        b"\x89PNG\r\n\x1a\n"
        + png_chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 2, 0, 0, 0))
        + png_chunk(b"IDAT", comp)
        + png_chunk(b"IEND", b"")
    )


def main():
    # 448x448:与 vision 模型输入尺寸一致,避免 server 额外缩放。
    w = h = 448
    orange = write_png(w, h, lambda x, y: (255, 128, 0))

    def square_px(x, y):
        if 144 <= x < 304 and 144 <= y < 304:
            return (0, 0, 0)
        return (255, 255, 255)

    square = write_png(w, h, square_px)

    out = {
        "orange": "data:image/png;base64," + base64.b64encode(orange).decode(),
        "square": "data:image/png;base64," + base64.b64encode(square).decode(),
    }
    path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_images.json")
    with open(path, "w") as f:
        json.dump(out, f)
    print("orange png bytes:", len(orange), "square png bytes:", len(square))
    print("orange data uri len:", len(out["orange"]))
    print("square data uri len:", len(out["square"]))
    print("saved:", path)


if __name__ == "__main__":
    main()