Movatterモバイル変換


[0]ホーム

URL:


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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
AALAM98mod100 wants to merge6 commits intomatplotlib:main
base:main
Choose a base branch
Loading
fromAALAM98mod100:alpha-array-support-imshow

Conversation

AALAM98mod100
Copy link

@AALAM98mod100AALAM98mod100 commentedAug 14, 2023
edited
Loading

PR summary

PR checklist

Plotting demo:

Code

# test alpha blending when both alpha and array alpha are passedimportmatplotlib.pyplotaspltfrommatplotlib.colorsimportListedColormapimportnumpyasnpfig,axs=plt.subplots(3,4,figsize=(12,10),layout='compressed')foraxinaxs.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.5alpha_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)forax,alpha,tinzip(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()

@melissawm
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

@AALAM98mod100AALAM98mod100 marked this pull request as ready for reviewAugust 18, 2023 21:15
@AALAM98mod100AALAM98mod100 marked this pull request as draftAugust 18, 2023 21:16
@oscargus
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

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 acheck_figures_equal test. Compare a (n, m, 3) array plus additionalalpha to the same plot with a(n, m, 4) array.

tacaswell reacted with thumbs up emoji
Copy link
Member

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

Choose a reason for hiding this comment

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

what input isnp.ndim(alpha) == 0 addressing?

Copy link
Author

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

@stevezhang1999
Copy link
Contributor

Just as a FYI, in the original issue thread, some incorrect result was observed usingsavefig (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

Choose a reason for hiding this comment

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

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

Copy link
Member

@tacaswelltacaswell 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 usecheck_figures_equal and the multiple application of alpha needs to be fixed.

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

@timhoffmtimhoffmtimhoffm left review comments

@tacaswelltacaswelltacaswell 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()
6 participants
@AALAM98mod100@melissawm@oscargus@stevezhang1999@tacaswell@timhoffm

[8]ページ先頭

©2009-2025 Movatter.jp