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

Commit146e9d0

Browse files
committed
Filter images in premultiplied alpha mode.
Test e.g. with```pythonimport matplotlib.pyplot as pltimport numpy as npfig = plt.figure(figsize=(6, 6))img = np.zeros((5, 5, 4))img[..., 1] = 1.img[..., 3] = np.tril(np.ones((5, 5)))fig.add_subplot(221).imshow(img, interpolation="bilinear")fig.add_subplot(222).imshow((img * 0xff).astype("u1"), interpolation="bilinear")fig.add_subplot(212).imshow( np.where( np.mgrid[:100, :200][0] > np.random.randint(100, size=200), np.arange(100)[:, None], np.nan), cmap="Greens", interpolation="antialiased", interpolation_stage="rgba")plt.show()```
1 parent9f7b3dd commit146e9d0

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

‎lib/matplotlib/image.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -496,37 +496,54 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
496496
out_alpha*=_resample(self,alpha,out_shape,t,resample=True)
497497
# mask and run through the norm
498498
resampled_masked=np.ma.masked_array(A_resampled,out_mask)
499-
output=self.norm(resampled_masked)
499+
res=self.norm(resampled_masked)
500500
else:
501501
ifA.ndim==2:# interpolation_stage = 'rgba'
502502
self.norm.autoscale_None(A)
503503
A=self.to_rgba(A)
504504
alpha=self.get_alpha()
505+
post_apply_alpha=False
505506
ifalphaisNone:# alpha parameter not specified
506507
ifA.shape[2]==3:# image has no alpha channel
507-
output_alpha=255ifA.dtype==np.uint8else1.0
508-
else:
509-
output_alpha=_resample(# resample alpha channel
510-
self,A[...,3],out_shape,t)
511-
output=_resample(# resample rgb channels
512-
self,_rgb_to_rgba(A[..., :3]),out_shape,t)
508+
alpha=np.uint8(0xff)ifA.dtype==np.uint8else1.0
509+
A=np.dstack([A,np.full(A.shape[:2],alpha)])
513510
elifnp.ndim(alpha)>0:# Array alpha
514511
# user-specified array alpha overrides the existing alpha channel
515-
output_alpha=_resample(self,alpha,out_shape,t)
516-
output=_resample(
517-
self,_rgb_to_rgba(A[..., :3]),out_shape,t)
512+
A=np.dstack([A[..., :3],alpha])
518513
else:# Scalar alpha
519514
ifA.shape[2]==3:# broadcast scalar alpha
520-
output_alpha= (255*alpha)ifA.dtype==np.uint8elsealpha
515+
ifA.dtype==np.uint8:
516+
alpha=np.uint8(0xff*alpha)
517+
A=np.dstack([A,np.full(A.shape[:2],alpha)])
521518
else:# or apply scalar alpha to existing alpha channel
522-
output_alpha=_resample(self,A[...,3],out_shape,t)*alpha
523-
output=_resample(
524-
self,_rgb_to_rgba(A[..., :3]),out_shape,t)
525-
output[...,3]=output_alpha# recombine rgb and alpha
526-
527-
# output is now either a 2D array of normed (int or float) data
519+
post_apply_alpha=True
520+
# Resample in premultiplied alpha space. (TODO: Consider
521+
# implementing premultiplied-space resampling in
522+
# span_image_resample_rgba_affine::generate?)
523+
ifA.dtype==np.uint8:
524+
# Temporarily increase bitdepth to avoid precision loss.
525+
A=A.astype(np.uint16)
526+
A[..., :3]*=A[...,3:]
527+
A[...,3]*=np.uint16(0x100)
528+
res=_resample(self,A,out_shape,t)
529+
out_alpha=res[...,3:]/0x100
530+
rgb_pre=res[..., :3]
531+
rgb=rgb_pre/np.where(out_alpha,out_alpha,1)
532+
res=np.dstack([rgb,out_alpha])
533+
ifpost_apply_alpha:
534+
res[...,3]*=alpha
535+
res=np.minimum(res.round(),0xff).astype(np.uint8)
536+
else:
537+
A[..., :3]*=A[...,3:]
538+
res=_resample(self,A,out_shape,t)
539+
np.divide(res[..., :3],res[...,3:],out=res[..., :3],
540+
where=res[...,3:]!=0)
541+
ifpost_apply_alpha:
542+
res[...,3]*=alpha
543+
544+
# res is now either a 2D array of normed (int or float) data
528545
# or an RGBA array of re-sampled input
529-
output=self.to_rgba(output,bytes=True,norm=False)
546+
output=self.to_rgba(res,bytes=True,norm=False)
530547
# output is now a correctly sized RGBA array of uint8
531548

532549
# Apply alpha *after* if the input was greyscale without a mask

‎lib/matplotlib/tests/test_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def test_image_composite_background():
514514
ax.set_xlim([0,12])
515515

516516

517-
@image_comparison(['image_composite_alpha'],remove_text=True)
517+
@image_comparison(['image_composite_alpha'],remove_text=True,tol=0.07)
518518
deftest_image_composite_alpha():
519519
"""
520520
Tests that the alpha value is recognized and correctly applied in the

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp