Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docling/utils/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def draw_clusters(
cx0, cy0, cx1, cy1 = tc.rect.to_bounding_box().as_tuple()
cx0 *= scale_x
cx1 *= scale_x
cy0 *= scale_x
cy0 *= scale_y
cy1 *= scale_y

draw.rectangle(
Expand All @@ -40,7 +40,7 @@ def draw_clusters(
x0, y0, x1, y1 = c.bbox.as_tuple()
x0 *= scale_x
x1 *= scale_x
y0 *= scale_x
y0 *= scale_y
y1 *= scale_y

if y1 <= y0:
Expand Down
37 changes: 37 additions & 0 deletions tests/test_visualization_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from docling_core.types.doc import BoundingBox, CoordOrigin, DocItemLabel

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anxkhn this looks good but we would prefer to drop this test

from PIL import Image, ImageDraw

from docling.datamodel.base_models import Cluster
from docling.utils.visualization import draw_clusters


def test_draw_clusters_scales_y_edges_with_scale_y(monkeypatch):
"""The top and bottom cluster edges must both scale by scale_y.

Under non-uniform scaling (scale_x != scale_y) the top y-coordinate was
scaled by scale_x, misplacing the top edge of every drawn box. Capture the
coordinates handed to ImageDraw.rectangle and assert both y edges use
scale_y.
"""
captured: list = []
original_rectangle = ImageDraw.ImageDraw.rectangle

def capturing_rectangle(self, xy, *args, **kwargs):
captured.append(xy)
return original_rectangle(self, xy, *args, **kwargs)

monkeypatch.setattr(ImageDraw.ImageDraw, "rectangle", capturing_rectangle)

bbox = BoundingBox(l=10, t=20, r=30, b=40, coord_origin=CoordOrigin.TOPLEFT)
cluster = Cluster(id=0, label=DocItemLabel.TEXT, bbox=bbox)
image = Image.new("RGB", (200, 300))

scale_x, scale_y = 2.0, 5.0
draw_clusters(image, [cluster], scale_x, scale_y)

# The first rectangle is the cluster box: [(x0, y0), (x1, y1)].
(x0, y0), (x1, y1) = captured[0]
assert x0 == bbox.l * scale_x
assert x1 == bbox.r * scale_x
assert y0 == bbox.t * scale_y
assert y1 == bbox.b * scale_y
Loading