Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add alpha-array support to _rgb_to_rgba #26520

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

Open
AALAM98mod100 wants to merge 6 commits into matplotlib:main
base: main
Choose a base branch
Loading
from AALAM98mod100:alpha-array-support-imshow

Conversation

Copy link

@AALAM98mod100 AALAM98mod100 commented Aug 14, 2023
edited
Loading

PR summary

PR checklist

Plotting demo:

Code

# test alpha blending when both alpha and array alpha are passed
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import numpy as np
fig, axs = plt.subplots(3, 4, figsize=(12, 10), layout='compressed')
for ax in axs.flat:
 ax.set(facecolor='red', xticks=[], yticks=[])
mapped = np.array([
 [0.1, 1.0],
 [1.0, 0.1]
])
rgb = np.repeat(mapped[:, :, np.newaxis], 3, axis=2)
rgba = np.concatenate(
 [
 rgb,
 [
 [[1.0], [0.9]],
 [[0.8], [0.7]],
 ]
 ],
 axis=2
)
alpha_scalar = 0.5
alpha_2d = np.full_like(mapped, alpha_scalar)
cmap_with_alpha = ListedColormap(
 np.concatenate([plt.cm.viridis.colors,
 np.full((len(plt.cm.viridis.colors), 1), alpha_scalar)],
 axis=1),
)
# axs[0,0].imshow(rgb, alpha=alpha_scalar)
# axs[0,1].imshow(rgb, alpha=alpha_2d)
for ax, alpha, t in zip(axs, [None, alpha_scalar, alpha_2d], ['off', 'float', 'array']):
 ax[0].imshow(mapped, alpha=alpha)
 ax[0].set_title(f'2D, alpha={alpha_scalar} {t}')
 ax[1].imshow(mapped, cmap=cmap_with_alpha, alpha=alpha)
 ax[1].set_title(f'2D with {alpha_scalar} alpha cmap, alpha={alpha_scalar} {t}')
 ax[2].imshow(rgb, alpha=alpha)
 ax[2].set_title(f'RGB, alpha={alpha_scalar} {t}')
 ax[3].imshow(rgba, alpha=alpha)
 ax[3].set_title(f'RGBA, alpha={alpha_scalar} {t}')
plt.show()

Copy link
Member

Hi @AALAM98mod100 - just leaving a note that if you want this to be reviewed make sure you mark it as "Ready for review". Cheers!

AALAM98mod100 reacted with thumbs up emoji

@AALAM98mod100 AALAM98mod100 marked this pull request as ready for review August 18, 2023 21:15
@AALAM98mod100 AALAM98mod100 marked this pull request as draft August 18, 2023 21:16
Copy link
Member

Thanks for the PR! As far as I can tell, it looks good!

However, I am a bit surprised to see that it doesn't work for SVGs? Especially since there is an embedded PNG in the SVG. This may be a another issue though... But do you have any comments on that? Maybe we should wait with adding SVG and PDF test images (although they are quite small, so not sure if it really is a problem).

Right now we are in the final stages of releasing 3.8, so it may take some time to get additional feedback.

Comment on lines +271 to +284
@image_comparison(['image_alpha_array'], remove_text=True)
def test_image_alpha_array():
_, ax = plt.subplots()

# create a 2x2 grid. If alpha is applying correctly,
# the top half should not match the 0.5 valued pixel
# in the bottom half.
arr = np.array([[.5, .5], [.75, .5]])
cmap = plt.get_cmap('gray')
norm = colors.Normalize()
arr_rgb = cmap(norm(arr))[:, :, :3]
alpha = np.ones_like(arr)
alpha[:1] = 0.2
ax.imshow(arr, alpha=alpha, cmap='gray', interpolation='none')
Copy link
Member

@timhoffm timhoffm Sep 1, 2023

Choose a reason for hiding this comment

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

This can be better written as a check_figures_equal test. Compare a (n, m, 3) array plus additional alpha to the same plot with a (n, m, 4) array.

tacaswell reacted with thumbs up emoji
Copy link
Member

@tacaswell tacaswell Sep 18, 2023

Choose a reason for hiding this comment

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

It is also concerning that the png and svg images are different....

"""
Convert an RGB image to RGBA, as required by the image resample C++
extension.
"""
rgba = np.zeros((A.shape[0], A.shape[1], 4), dtype=A.dtype)
if alpha is None or np.ndim(alpha) == 0:
Copy link
Member

@timhoffm timhoffm Sep 1, 2023

Choose a reason for hiding this comment

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

what input is np.ndim(alpha) == 0 addressing?

Copy link
Author

@AALAM98mod100 AALAM98mod100 Sep 7, 2023

Choose a reason for hiding this comment

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

Scalar alpha input should remaing unaffected by new code. This ensures just that

Copy link
Contributor

Just as a FYI, in the original issue thread, some incorrect result was observed using savefig (I haven't tested it yet)

elif A.shape[2] == 4:
array_alpha = self.get_alpha()
if array_alpha is not None and np.ndim(array_alpha) != 0:
A[:, :, 3] *= array_alpha # blend alphas of image and param
Copy link
Member

@tacaswell tacaswell Sep 18, 2023

Choose a reason for hiding this comment

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

This modifies A in place where as in the other branches we get a copy of A back. Because it modifies in in place and we cache A higher up in the call stack it will apply the alpha everytime it draws.

Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

The tests should use check_figures_equal and the multiple application of alpha needs to be fixed.

AALAM98mod100 reacted with thumbs up emoji
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@timhoffm timhoffm timhoffm left review comments

@tacaswell tacaswell tacaswell requested changes

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Projects
Status: Waiting for author
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

[Bug]: alpha array-type not working with RGB image in imshow()

AltStyle によって変換されたページ (->オリジナル) /