109 lines
4.9 KiB
Python
109 lines
4.9 KiB
Python
"""Preserve source bytes and their actual MIME types across API endpoints."""
|
|
import base64
|
|
import hashlib
|
|
import io
|
|
from PIL import Image
|
|
|
|
MAX_IMAGES = 5
|
|
MAX_IMAGE_BYTES = 20 * 1024 * 1024
|
|
|
|
|
|
def normalize_images(value):
|
|
images = [value] if isinstance(value, str) else (value or [])
|
|
if not isinstance(images, list) or any(not isinstance(item, str) for item in images):
|
|
raise ValueError("images 必须为 base64 图片数组")
|
|
if len(images) > MAX_IMAGES:
|
|
raise ValueError(f"最多上传 {MAX_IMAGES} 张图片")
|
|
result = []
|
|
for source in images:
|
|
payload = source.split(",", 1)[1] if source.startswith("data:") and "," in source else source
|
|
if len(payload) > MAX_IMAGE_BYTES * 4 // 3 + 8:
|
|
raise ValueError("单张图片不能超过 20 MB")
|
|
try:
|
|
data = base64.b64decode(payload, validate=True)
|
|
except Exception as exc:
|
|
raise ValueError("图片不是合法 base64") from exc
|
|
if data.startswith(b"\x89PNG\r\n\x1a\n"):
|
|
mime, ext = "image/png", "png"
|
|
elif data.startswith(b"\xff\xd8\xff"):
|
|
mime, ext = "image/jpeg", "jpg"
|
|
elif data[:4] == b"RIFF" and data[8:12] == b"WEBP":
|
|
mime, ext = "image/webp", "webp"
|
|
else:
|
|
raise ValueError("仅支持 PNG、JPEG、WebP 图片")
|
|
result.append({"data": data, "mime": mime, "ext": ext,
|
|
"sha256": hashlib.sha256(data).hexdigest(),
|
|
"url": f"data:{mime};base64,{base64.b64encode(data).decode()}"})
|
|
return result
|
|
|
|
|
|
def response_content(text, images):
|
|
return [{"type": "input_text", "text": text}] + [
|
|
{"type": "input_image", "image_url": image["url"], "detail": "high"}
|
|
for image in images
|
|
]
|
|
|
|
|
|
def image_inventory(images):
|
|
result = []
|
|
for index, image in enumerate(images, 1):
|
|
with Image.open(io.BytesIO(image["data"])) as source:
|
|
result.append({"image_index": index, "width": source.width, "height": source.height})
|
|
return result
|
|
|
|
|
|
def crop_reference(images, image_index, box):
|
|
if type(image_index) is not int or not 1 <= image_index <= len(images):
|
|
raise ValueError("image_index 必须是已有原图的 1-based 编号")
|
|
if not isinstance(box, list) or len(box) != 4 or any(type(v) is not int for v in box):
|
|
raise ValueError("box 必须为四个整数像素坐标 [left, top, right, bottom]")
|
|
source_image = images[image_index - 1]
|
|
with Image.open(io.BytesIO(source_image["data"])) as source:
|
|
left, top, right, bottom = box
|
|
if not (0 <= left < right <= source.width and 0 <= top < bottom <= source.height):
|
|
raise ValueError("裁切框必须在原图像素范围内且面积为正")
|
|
buffer = io.BytesIO()
|
|
source.crop(tuple(box)).save(buffer, format="PNG")
|
|
return {"image_index": image_index, "box": box, "source_sha256": source_image["sha256"]}, buffer.getvalue()
|
|
|
|
|
|
def drawing_regions(images, max_regions=8):
|
|
"""Locate separated ink regions, without recognizing a part or changing pixels.
|
|
|
|
Whole sheets always remain available: this heuristic may split a view or find
|
|
a notes block. No scale or dimensions are inferred from the crop boundaries.
|
|
"""
|
|
import numpy as np
|
|
from scipy import ndimage
|
|
found = []
|
|
for index, image in enumerate(images, 1):
|
|
with Image.open(io.BytesIO(image['data'])) as source:
|
|
if min(source.size) < 400:
|
|
continue
|
|
scale = min(1, 1100 / max(source.size))
|
|
small = source.convert('L').resize((round(source.width*scale), round(source.height*scale)))
|
|
ink = np.asarray(small) < 185
|
|
# Do not group the page frame and title table into a full-page crop.
|
|
rows = ink.sum(axis=1) > ink.shape[1]*.65
|
|
cols = ink.sum(axis=0) > ink.shape[0]*.65
|
|
ink[ndimage.binary_dilation(rows, iterations=2), :] = False
|
|
ink[:, ndimage.binary_dilation(cols, iterations=2)] = False
|
|
joined = ndimage.maximum_filter(ink, size=17)
|
|
labels, _ = ndimage.label(joined)
|
|
regions = []
|
|
for region in ndimage.find_objects(labels):
|
|
if region is None: continue
|
|
y, x = region
|
|
width, height = x.stop-x.start, y.stop-y.start
|
|
if min(width,height) < 65 or max(width/height,height/width) > 4:
|
|
continue
|
|
area = width*height
|
|
if area > ink.size*.55 or area < ink.size*.009:
|
|
continue
|
|
pad = 20
|
|
box = [max(0, int((x.start-pad)/scale)), max(0, int((y.start-pad)/scale)),
|
|
min(source.width, int((x.stop+pad)/scale)), min(source.height, int((y.stop+pad)/scale))]
|
|
regions.append((area, {'image_index': index, 'box': box}))
|
|
found.extend(item[1] for item in sorted(regions, key=lambda r: -r[0])[:4])
|
|
return found[:max_regions]
|