Perf: Replace slow pixel-by-pixel color loop with Image.composite
The PillowResult.get_image method used a slow, pure-Python loop to apply styles, causing significant performance degradation on large images. This commit replaces that implementation with a single, highly-optimized call to Image.composite, using the rendered B&W image as a mask. This reduces rendering time by over 90% in test cases.
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 86.25%. Comparing base (92a13a0) to head (07ced97).
Additional details and impacted files
@@ Coverage Diff @@
## main #391 +/- ##
==========================================
- Coverage 86.25% 86.25% -0.01%
==========================================
Files 165 165
Lines 7831 7821 -10
==========================================
- Hits 6755 6746 -9
+ Misses 1076 1075 -1
| Flag | Coverage Δ | |
|---|---|---|
| all/ubuntu-22.04/3.13 | 86.25% <100.00%> (-0.01%) |
:arrow_down: |
| all/ubuntu-22.04/3.8 | 86.02% <100.00%> (-0.01%) |
:arrow_down: |
| all/ubuntu-24.04/3.13 | 86.25% <100.00%> (-0.01%) |
:arrow_down: |
| all/ubuntu-24.04/3.8 | 86.02% <100.00%> (-0.01%) |
:arrow_down: |
| all/windows-2022/3.13 | 86.25% <100.00%> (-0.01%) |
:arrow_down: |
| all/windows-2022/3.8 | 86.02% <100.00%> (-0.01%) |
:arrow_down: |
| all/windows-2025/3.13 | 86.25% <100.00%> (-0.01%) |
:arrow_down: |
| all/windows-2025/3.8 | 86.02% <100.00%> (-0.01%) |
:arrow_down: |
| e2e/ubuntu-24.04/3.10 | 60.33% <100.00%> (-0.04%) |
:arrow_down: |
| e2e/ubuntu-24.04/3.11 | 60.33% <100.00%> (-0.04%) |
:arrow_down: |
| e2e/ubuntu-24.04/3.12 | 60.33% <100.00%> (-0.04%) |
:arrow_down: |
| e2e/ubuntu-24.04/3.9 | 59.67% <100.00%> (-0.04%) |
:arrow_down: |
| e2e/windows-2022/3.10 | 60.33% <100.00%> (-0.04%) |
:arrow_down: |
| e2e/windows-2022/3.11 | 60.33% <100.00%> (-0.04%) |
:arrow_down: |
| e2e/windows-2022/3.12 | 60.33% <100.00%> (-0.04%) |
:arrow_down: |
| e2e/windows-2022/3.9 | 59.67% <100.00%> (-0.04%) |
:arrow_down: |
| unit/ubuntu-22.04/3.10 | 82.98% <100.00%> (-0.01%) |
:arrow_down: |
| unit/ubuntu-22.04/3.11 | 82.98% <100.00%> (-0.01%) |
:arrow_down: |
| unit/ubuntu-22.04/3.12 | 82.98% <100.00%> (-0.01%) |
:arrow_down: |
| unit/ubuntu-22.04/3.9 | 82.69% <100.00%> (-0.01%) |
:arrow_down: |
| unit/ubuntu-24.04/3.10 | 82.98% <100.00%> (-0.01%) |
:arrow_down: |
| unit/ubuntu-24.04/3.11 | 82.98% <100.00%> (-0.01%) |
:arrow_down: |
| unit/ubuntu-24.04/3.12 | 82.98% <100.00%> (-0.01%) |
:arrow_down: |
| unit/ubuntu-24.04/3.9 | 82.69% <100.00%> (-0.01%) |
:arrow_down: |
| unit/windows-2022/3.10 | 82.98% <100.00%> (-0.01%) |
:arrow_down: |
| unit/windows-2022/3.11 | 82.98% <100.00%> (-0.01%) |
:arrow_down: |
| unit/windows-2022/3.12 | 82.98% <100.00%> (-0.01%) |
:arrow_down: |
| unit/windows-2022/3.9 | 82.69% <100.00%> (-0.01%) |
:arrow_down: |
| unit/windows-2025/3.10 | 82.98% <100.00%> (-0.01%) |
:arrow_down: |
| unit/windows-2025/3.11 | 82.98% <100.00%> (-0.01%) |
:arrow_down: |
| unit/windows-2025/3.12 | 82.98% <100.00%> (-0.01%) |
:arrow_down: |
| unit/windows-2025/3.9 | 82.69% <100.00%> (-0.01%) |
:arrow_down: |
Flags with carried forward coverage won't be shown. Click here to find out more.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
Hi, thank you for your PR. I will try to find time to review and benchmark this soon.
Please fix all the code style issues reported by ruff. For a quick guide on how to set up a local dev environment please check out Development documentation. At the moment only style issues reported is a formatting issue which can be auto-fixed with a single command:
poe test-style
Please fix all the code style issues reported by ruff. For a quick guide on how to set up a local dev environment please check out Development documentation. At the moment only style issues reported is a formatting issue which can be auto-fixed with a single command:
poe test-style
Thanks for the pointers!
I’ve set up the local dev env per the docs and ran:
poe test-style
This applied the ruff-format changes that CI flagged. I’ve committed and pushed those fixes, so the Style check should pass now.
The PR is ready for your review/benchmarking. I’m happy to tweak anything further (naming, comments, tests, etc.) if you prefer a different approach.