Skip to content

image(): align the scaled image, not its box, with keep_aspect_ratio and x="C"/"R"/"L" - #2136

Merged
andersonhc merged 2 commits into
py-pdf:masterfrom
vladosskins178:fix/image-align-keep-aspect-ratio
Nov 10, 2026
Merged

andersonhc merged 2 commits into
py-pdf:masterfrom
vladosskins178:fix/image-align-keep-aspect-ratio

Conversation

@vladosskins178

Copy link
Copy Markdown

Fixes #2131

When x is "L", "C" or "R" and keep_aspect_ratio=True, x_by_align() already returns the x of the scaled image. _raster_image() and _vector_image() then passed that x to scale_inside_box() with the full box width, which added half of the unused width a second time. Now the x from x_by_align() is kept and only y, w and h come from scale_inside_box(). A numeric x is unchanged: the image is still centered in the box.

With a square image in a 100x50 box on A4 (left / right edge of the image, in mm):

x before after
"L" 35 / 85 10 / 60
"C" 105 / 155 80 / 130
"R" 175 / 225 150 / 200

An image that fills the box width (e.g. a wide image in the same box) is placed the same as before.

Checklist:

  • A unit test is covering the code added / modified by this PR: test_image_fit_in_rect_with_x_align (PNG and SVG, each with L / C / R). It fails on master and passes with the fix.

  • In case of a new feature, docstrings have been added, with also some documentation in the docs/ folder: N/A, bug fix

  • A mention of the change is present in CHANGELOG.md

  • This PR is ready to be merged

Full test suite: 2639 passed, 3 skipped, 4 xfailed (Python 3.12, with qpdf). black, pylint, mypy clean; pyright shows only the same 3 missing-module errors as master here (endesive, uharfbuzz not installed).

By submitting this pull request, I confirm that my contribution is made under the terms of the GNU LGPL 3.0 license.

@andersonhc

Copy link
Copy Markdown
Collaborator

@allcontributors please add @vladosskins178 for code

@andersonhc
andersonhc merged commit 00ee473 into py-pdf:master Nov 10, 2026
40 of 42 checks passed
@allcontributors

Copy link
Copy Markdown

@andersonhc

I've put up a pull request to add @vladosskins178! 🎉

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.

FPDF.image() with x="C"/x="R" and keep_aspect_ratio=True offsets the image when it is narrower than the box

2 participants