Respect object bounds when trimming labels
This commit is contained in:
+7
-2
@@ -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))
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user