Skip to content

Filter images in premultiplied alpha mode. #29776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,37 +496,40 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
out_alpha *= _resample(self, alpha, out_shape, t, resample=True)
# mask and run through the norm
resampled_masked = np.ma.masked_array(A_resampled, out_mask)
output = self.norm(resampled_masked)
res = self.norm(resampled_masked)
else:
if A.ndim == 2: # interpolation_stage = 'rgba'
self.norm.autoscale_None(A)
A = self.to_rgba(A)
if A.dtype == np.uint8:
# uint8 is too imprecise for premultiplied alpha roundtrips.
A = np.divide(A, 0xff, dtype=np.float32)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to convert here and pay the additional memory cost, or rather be defensive in the change and just not premultiply uint8 in this case?

Ping @QuLogic who looked deeper into memory usage in #29453.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rendering without premultiplying in the uint8 case would then be different compared to the float case, which would be really bad.
If memory is an issue I suspect we should write a C++ variant of span_image_resample_rgba_foo::generate that does the premultiplication at that stage, it seems doable...
I suspect this PR would supersede #29453 as it should remove the problematic parts targeted by it?

alpha = self.get_alpha()
post_apply_alpha = False
if alpha is None: # alpha parameter not specified
if A.shape[2] == 3: # image has no alpha channel
output_alpha = 255 if A.dtype == np.uint8 else 1.0
else:
output_alpha = _resample( # resample alpha channel
self, A[..., 3], out_shape, t)
output = _resample( # resample rgb channels
self, _rgb_to_rgba(A[..., :3]), out_shape, t)
A = np.dstack([A, np.ones(A.shape[:2])])
elif np.ndim(alpha) > 0: # Array alpha
# user-specified array alpha overrides the existing alpha channel
output_alpha = _resample(self, alpha, out_shape, t)
output = _resample(
self, _rgb_to_rgba(A[..., :3]), out_shape, t)
A = np.dstack([A[..., :3], alpha])
else: # Scalar alpha
if A.shape[2] == 3: # broadcast scalar alpha
output_alpha = (255 * alpha) if A.dtype == np.uint8 else alpha
A = np.dstack([A, np.full(A.shape[:2], alpha, np.float32)])
else: # or apply scalar alpha to existing alpha channel
output_alpha = _resample(self, A[..., 3], out_shape, t) * alpha
output = _resample(
self, _rgb_to_rgba(A[..., :3]), out_shape, t)
output[..., 3] = output_alpha # recombine rgb and alpha

# output is now either a 2D array of normed (int or float) data
post_apply_alpha = True
# Resample in premultiplied alpha space. (TODO: Consider
# implementing premultiplied-space resampling in
# span_image_resample_rgba_affine::generate?)
A[..., :3] *= A[..., 3:]
res = _resample(self, A, out_shape, t)
np.divide(res[..., :3], res[..., 3:], out=res[..., :3],
where=res[..., 3:] != 0)
if post_apply_alpha:
res[..., 3] *= alpha

# res is now either a 2D array of normed (int or float) data
# or an RGBA array of re-sampled input
output = self.to_rgba(output, bytes=True, norm=False)
output = self.to_rgba(res, bytes=True, norm=False)
# output is now a correctly sized RGBA array of uint8

# Apply alpha *after* if the input was greyscale without a mask
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,15 @@ def test_image_alpha():
def test_imshow_alpha(fig_test, fig_ref):
np.random.seed(19680801)

rgbf = np.random.rand(6, 6, 3)
rgbf = np.random.rand(6, 6, 3).astype(np.float32)
rgbu = np.uint8(rgbf * 255)
((ax0, ax1), (ax2, ax3)) = fig_test.subplots(2, 2)
ax0.imshow(rgbf, alpha=0.5)
ax1.imshow(rgbf, alpha=0.75)
ax2.imshow(rgbu, alpha=0.5)
ax3.imshow(rgbu, alpha=0.75)
ax2.imshow(rgbu, alpha=127/255)
ax3.imshow(rgbu, alpha=191/255)

rgbaf = np.concatenate((rgbf, np.ones((6, 6, 1))), axis=2)
rgbaf = np.concatenate((rgbf, np.ones((6, 6, 1))), axis=2).astype(np.float32)
rgbau = np.concatenate((rgbu, np.full((6, 6, 1), 255, np.uint8)), axis=2)
((ax0, ax1), (ax2, ax3)) = fig_ref.subplots(2, 2)
rgbaf[:, :, 3] = 0.5
Expand Down Expand Up @@ -514,7 +514,7 @@ def test_image_composite_background():
ax.set_xlim([0, 12])


@image_comparison(['image_composite_alpha'], remove_text=True)
@image_comparison(['image_composite_alpha'], remove_text=True, tol=0.07)
def test_image_composite_alpha():
"""
Tests that the alpha value is recognized and correctly applied in the
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from matplotlib import cm, pyplot as plt


@image_comparison(['pngsuite.png'], tol=0.04)
@image_comparison(['pngsuite.png'], tol=0.09)
def test_pngsuite():
files = sorted(
(Path(__file__).parent / "baseline_images/pngsuite").glob("basn*.png"))
Expand Down
Loading