Improve image inset resampling quality#1077
Merged
Merged
Conversation
Pre-resize the source to the inset's exact device-pixel footprint with PIL and render it pixel-for-pixel (interpolation='none'), instead of letting matplotlib resample the full-resolution image. Downscale with LANCZOS to antialias (matplotlib's resampler aliases on large reductions); enlarge with NEAREST to stay crisp (smooth filters look hazy and ring on hard edges).
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1077 +/- ##
==========================================
- Coverage 87.07% 87.07% -0.01%
==========================================
Files 208 208
Lines 14341 14355 +14
Branches 1789 1791 +2
==========================================
+ Hits 12488 12500 +12
- Misses 1286 1287 +1
- Partials 567 568 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Image insets (
inset_elementwith aPIL.Imageornumpy.ndarray) previouslyhanded the full-resolution image to matplotlib's
BboxImageand let it resampleto the inset's size at render time. matplotlib's resampler aliases badly on large
downscales — the regime an inset typically hits — producing soft, moiré-ridden
images.
This change pre-resizes the source to the inset's exact device-pixel footprint
with PIL and renders it pixel-for-pixel (
interpolation="none"), choosing thefilter by scaling direction.
Resampling policy
LANCZOSNEARESTImplementation notes
PIL.Imageonce, on construction(
_to_pil_image); a float ndarray follows matplotlib's[0, 1]convention._arrange_in_box, notdraw(): the device-pixel size isonly known after the layout engine finalises the inset's bbox at render time.
draw()just creates theBboxImage;_arrange_in_boxsets its data.Baselines
Updated the affected image-inset baselines (
image_aspect_fit_top_right,image_aspect_fit_bottom,image_standalone) to reflect the new resampling.