项目文件夹

文件
wehub-resource-sync 925e56bb5f
Unit tests / build (t4_gpu) (push) Has been cancelled
Unit tests / build (ubuntu-latest) (push) Has been cancelled
Unit tests / build (windows-latest) (push) Has been cancelled
Test CLI scripts / build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:56 +08:00

58 行
1.6 KiB
Python

此文件含有模棱两可的 Unicode 字符
此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。
from PIL import Image, ImageDraw
def test_table_rec(table_rec_predictor):
data = [
["Name", "Age", "City"],
["Alice", 25, "New York"],
["Bob", 30, "Los Angeles"],
["Charlie", 35, "Chicago"],
]
test_image = draw_table(data)
results = table_rec_predictor([test_image])
assert len(results) == 1
assert results[0].image_bbox == [0, 0, test_image.size[0], test_image.size[1]]
if results[0].error:
return
rows = results[0].rows
cols = results[0].cols
cells = results[0].cells
assert len(rows) >= 1
assert len(cols) >= 1
# Geometric cells = rows × cols
assert len(cells) == len(rows) * len(cols) or len(cells) <= len(rows) * len(cols)
def draw_table(data, cell_width=100, cell_height=40):
rows = len(data)
cols = len(data[0])
width = cols * cell_width
height = rows * cell_height
image = Image.new("RGB", (width, height), "white")
draw = ImageDraw.Draw(image)
for i in range(rows + 1):
y = i * cell_height
draw.line([(0, y), (width, y)], fill="black", width=1)
for i in range(cols + 1):
x = i * cell_width
draw.line([(x, 0), (x, height)], fill="black", width=1)
for i in range(rows):
for j in range(cols):
text = str(data[i][j])
text_bbox = draw.textbbox((0, 0), text)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
x = j * cell_width + (cell_width - text_width) // 2
y = i * cell_height + (cell_height - text_height) // 2
draw.text((x, y), text, fill="black")
return image