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

Fix array alpha to multiply (not replace) existing RGBA alpha#30795

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
FazeelUsmani wants to merge2 commits intomatplotlib:main
base:main
Choose a base branch
Loading
fromFazeelUsmani:fix-rgb-array-alpha-26092
Open
Show file tree
Hide file tree
Changes fromall 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
10 changes: 6 additions & 4 deletionsdoc/api/next_api_changes/behavior/28437-CH.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
*alpha* parameter handling on images
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When passingand array to ``imshow(..., alpha=...)``, the parameter was silently ignored
if the image data was a RGB orRBGA image or if :rc:`interpolation_state`
resolved to "rbga".
When passingan array to ``imshow(..., alpha=...)``, the parameter was silently ignored
if the image data was a RGB orRGBA image or if :rc:`interpolation_stage`
resolved to "rgba".

This is now fixed, and the alpha array overwrites any previous transparency information.
This is now fixed. For RGB images, the alpha array is used directly as the alpha channel.
For RGBA images, the alpha array is multiplied with the existing alpha channel, consistent
with how scalar alpha values are handled.
6 changes: 4 additions & 2 deletionslib/matplotlib/image.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -512,8 +512,10 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
if A.shape[2] == 3: # image has no alpha channel
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
A = np.dstack([A[..., :3], alpha])
if A.shape[2] == 3: # RGB: use array alpha directly
A = np.dstack([A, alpha])
else: # RGBA: multiply existing alpha by array alpha
A = np.dstack([A[..., :3], A[..., 3] * alpha])
else: # Scalar alpha
if A.shape[2] == 3: # broadcast scalar alpha
A = np.dstack([A, np.full(A.shape[:2], alpha, np.float32)])
Expand Down
7 changes: 4 additions & 3 deletionslib/matplotlib/tests/test_image.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1841,7 +1841,7 @@ def test_interpolation_stage_rgba_respects_alpha_param(fig_test, fig_ref, intp_s
axs_ref[0][2].imshow(im_rgba, interpolation_stage=intp_stage)

# When the image already has an alpha channel, multiply it by the
#scalaralpha param, or replace it bythearray alpha param
# alpha param (both scalar and array alpha multiplytheexisting alpha)
axs_tst[1][0].imshow(im_rgba)
axs_ref[1][0].imshow(im_rgb, alpha=array_alpha)
axs_tst[1][1].imshow(im_rgba, interpolation_stage=intp_stage, alpha=scalar_alpha)
Expand All@@ -1853,7 +1853,8 @@ def test_interpolation_stage_rgba_respects_alpha_param(fig_test, fig_ref, intp_s
new_array_alpha = np.random.rand(ny, nx)
axs_tst[1][2].imshow(im_rgba, interpolation_stage=intp_stage, alpha=new_array_alpha)
axs_ref[1][2].imshow(
np.concatenate( # combine rgb channels with new array alpha
(im_rgb, new_array_alpha.reshape((ny, nx, 1))), axis=-1
np.concatenate( # combine rgb channels with multiplied array alpha
(im_rgb, array_alpha.reshape((ny, nx, 1))
* new_array_alpha.reshape((ny, nx, 1))), axis=-1
), interpolation_stage=intp_stage
)
Loading

[8]ページ先頭

©2009-2025 Movatter.jp