Skip to content

Keep ImageOps.contain dimensions at least one pixel - #9998

Closed
ROTl24 wants to merge 1 commit into
python-pillow:mainfrom
ROTl24:fix/contain-minimum-dimension
Closed

Keep ImageOps.contain dimensions at least one pixel#9998
ROTl24 wants to merge 1 commit into
python-pillow:mainfrom
ROTl24:fix/contain-minimum-dimension

Conversation

@ROTl24

@ROTl24 ROTl24 commented Sep 13, 2026

Copy link
Copy Markdown

Fixes #9997.

Changes proposed in this pull request:

  • Keep the calculated dimension in ImageOps.contain() at least one pixel. For example, fitting a 100×1 image into 10×10 currently calculates a height of zero and raises ValueError. With this change it returns 10×1.
  • Cover horizontal and vertical narrow images, including the half-pixel rounding boundary. The tests also check pad() dimensions, image content, and background color.
  • Add a release note.

Validation on Windows / Python 3.13.15: all four new cases failed before the fix, and all 53 tests in Tests/test_imageops.py passed after it. These runs loaded this checkout's ImageOps.py against the installed Pillow 12.3.0 modules and binary core; I did not build main's C extensions or run the full suite. The installed core also passed the 59 selftests.

Black, Ruff, sphinx-lint, and git diff --check passed for the changed files. Black reported that the Python 3.13 runtime is older than the configured Python 3.15 target.

Prepared with OpenAI Codex assistance.

contain 在正数目标尺寸下可能把短边舍入为零,导致 resize 抛出异常,并连带影响 pad。将计算出的短边限制为至少一个像素,保留已有正常尺寸计算。

新增横向、纵向及半像素边界四组回归用例,检查 contain 尺寸和 pad 内容,并补充 13.0.0 发布说明。修复前四组失败,修复后 ImageOps 文件 53 项测试通过。验证采用当前 ImageOps 源码与已安装 Pillow 12.3.0 核心,未构建当前主分支 C 扩展或运行完整测试套件。已安装核心 selftest 59 项通过,Black、Ruff、sphinx-lint 和差异检查通过;Black 提示运行时低于目标 Python 版本。
@radarhere radarhere added the 🤖-assisted AI-assisted label Sep 13, 2026
@radarhere

Copy link
Copy Markdown
Member

This is a duplicate of #9672

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ImageOps.contain and pad fail when a narrow image rounds to zero pixels

2 participants