Perf: Replace slow pixel-by-pixel color loop with Image.composite#391
Conversation
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✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
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: |
Thanks for the pointers! I’ve set up the local dev env per the docs and ran: 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. |
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.