8000 Avoid an extra copy/resample if imshow input has no alpha by QuLogic · Pull Request #27562 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Avoid an extra copy/resample if imshow input has no alpha #27562

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
Jan 10, 2024
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
12 changes: 8 additions & 4 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,15 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
if A.ndim == 2: # _interpolation_stage == 'rgba'
self.norm.autoscale_None(A)
A = self.to_rgba(A)
if A.shape[2] == 3:
A = _rgb_to_rgba(A)
alpha = self._get_scalar_alpha()
output_alpha = _resample( # resample alpha channel
self, A[..., 3], out_shape, t, alpha=alpha)
if A.shape[2] == 3:
# No need to resample alpha or make a full array; NumPy will expand
# this out and cast to uint8 if necessary when it's assigned to the
# alpha channel below.
output_alpha = (255 * alpha) if A.dtype == np.uint8 else alpha
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't it be multiplying by 255 if it is a float, rather than if it is a uint8?

Copy link
Member Author
@QuLogic QuLogic Jan 9, 2024

Choose a reason for hiding this comment

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

alpha is always a 0-1 float; if the data is uint8, then output alpha must be 0-255 instead.

else:
output_alpha = _resample( # resample alpha channel
self, A[..., 3], out_shape, t, alpha=alpha)
output = _resample( # resample rgb channels
self, _rgb_to_rgba(A[..., :3]), out_shape, t, alpha=alpha)
output[..., 3] = output_alpha # recombine rgb and alpha
Expand Down
26 changes: 26 additions & 0 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,32 @@ def test_image_alpha():
ax3.imshow(Z, alpha=0.5, interpolation='nearest')


@mpl.style.context('mpl20')
@check_figures_equal(extensions=['png'])
def test_imshow_alpha(fig_test, fig_ref):
np.random.seed(19680801)

rgbf = np.random.rand(6, 6, 3)
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)

rgbaf = np.concatenate((rgbf, np.ones((6, 6, 1))), axis=2)
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
ax0.imshow(rgbaf)
rgbaf[:, :, 3] = 0.75
ax1.imshow(rgbaf)
rgbau[:, :, 3] = 127
ax2.imshow(rgbau)
rgbau[:, :, 3] = 191
ax3.imshow(rgbau)


def test_cursor_data():
from matplotlib.backend_bases import MouseEvent

Expand Down
0