Respect object bounds when trimming labels

This commit is contained in:
Mikei386
2026-06-22 16:25:16 +02:00
parent 26e1879104
commit 7243250abc
2 changed files with 8 additions and 2 deletions
+7 -2
View File
@@ -235,19 +235,24 @@ def crop_label_to_content(image: Image.Image, design: dict[str, Any], scale: flo
min_length_px = scaled_mm_to_px(10, scale)
black_mask = image.convert("L").point(lambda pixel: 255 if pixel < 128 else 0, "L")
bbox = black_mask.getbbox()
objects = [obj for obj in design.get("objects", []) if isinstance(obj, dict) and not obj.get("hidden")]
object_end = 0
for obj in objects:
x, y, w, h = object_box(obj, scale)
object_end = max(object_end, y + h if orientation == "portrait" else x + w)
if orientation == "portrait":
fixed_width = scaled_mm_to_px(tape_width, scale)
if not bbox:
return new_label_image(fixed_width, min_length_px)
end = min(image.height, bbox[3] + margin_px)
end = min(image.height, max(bbox[3], object_end) + margin_px)
target = min(image.height, max(min_length_px, end))
return image.crop((0, 0, image.width, target))
fixed_height = scaled_mm_to_px(tape_width, scale)
if not bbox:
return new_label_image(min_length_px, fixed_height)
end = min(image.width, bbox[2] + margin_px)
end = min(image.width, max(bbox[2], object_end) + margin_px)
target = min(image.width, max(min_length_px, end))
return image.crop((0, 0, target, image.height))
+1
View File
@@ -131,6 +131,7 @@ function objectContentEndMm(obj) {
if (obj.hidden) return 0;
if (state.design.orientation === "portrait") return obj.y + obj.h;
if (obj.type !== "text" && obj.type !== "symbol") return obj.x + obj.w;
if (obj.manualSize) return obj.x + obj.w;
const { value } = objectFont(obj);
ctx.save();