Skip to content

fix(visualization): scale top y-edge by scale_y in draw_clusters#3790

Open
anxkhn wants to merge 1 commit into
docling-project:mainfrom
anxkhn:patch-12
Open

fix(visualization): scale top y-edge by scale_y in draw_clusters#3790
anxkhn wants to merge 1 commit into
docling-project:mainfrom
anxkhn:patch-12

Conversation

@anxkhn

@anxkhn anxkhn commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

In draw_clusters the top y-coordinate of debug cluster boxes and their cells
was multiplied by scale_x, while the matching bottom y-coordinate on the next
line correctly used scale_y:

cx0 *= scale_x
cx1 *= scale_x
cy0 *= scale_x   # top edge - wrong axis
cy1 *= scale_y   # bottom edge

scale_x = image.width / page.size.width and
scale_y = image.height / page.size.height differ whenever the rasterized image
aspect ratio does not match the page aspect ratio (for example rendering/OCR at a
DPI that does not preserve aspect). In that case the top edge of every drawn box
is misplaced, distorting the overlay (and for a box whose scaled top passes its
bottom it can invert).

This changes the two top-edge lines to use scale_y, so both y-edges scale by
scale_y. That matches the pattern already used consistently elsewhere in the
repo (table_structure_model.py, table_structure_model_v2.py,
layout_object_detection_model.py) and the earlier fix for the same axis bug in
the table model (#2287).

The change only affects debug visualization overlays gated behind the debug
settings (visualize_cells / visualize_raw_layout / visualize_layout); the
DoclingDocument conversion output is unchanged, and it is a no-op under uniform
scaling (scale_x == scale_y), so no reference data is regenerated.

A focused regression test captures the coordinates handed to
ImageDraw.rectangle under non-uniform scaling and asserts both y-edges equal
bbox.t/b * scale_y.

Checklist:

  • Documentation has been updated, if necessary.
  • Examples have been added, if necessary.
  • Tests have been added, if necessary.

The top y-coordinate of debug cluster boxes and cells in draw_clusters
was multiplied by scale_x while the matching bottom y-coordinate used
scale_y. When the rasterized image aspect ratio differs from the page
aspect ratio (scale_x != scale_y), this misplaces the top edge of every
drawn rectangle.

Scale both y-edges with scale_y, matching the correct pattern already
used in the table structure and layout models. This only affects debug
visualization overlays gated behind the debug settings; conversion
output is unchanged.

Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

DCO Check Passed

Thanks @anxkhn, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@PeterStaar-IBM PeterStaar-IBM requested a review from cau-git July 11, 2026 12:09
@codecov

codecov Bot commented Jul 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
docling/utils/visualization.py 50.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@@ -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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants