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

Commitbba7763

Browse files
authored
Merge pull request#15437 from anntzer/jpegalpha
Improve handling of alpha when saving to jpeg.
2 parents38da016 +757fe20 commitbba7763

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

‎lib/matplotlib/backends/backend_agg.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -551,14 +551,15 @@ def print_jpg(self, filename_or_obj, *args, dryrun=False, pil_kwargs=None,
551551
`PIL.Image.Image.save` when saving the figure. These take
552552
precedence over *quality*, *optimize* and *progressive*.
553553
"""
554-
FigureCanvasAgg.draw(self)
554+
# Remove transparency by alpha-blending on an assumed white background.
555+
r,g,b,a=mcolors.to_rgba(self.figure.get_facecolor())
556+
try:
557+
self.figure.set_facecolor(a*np.array([r,g,b])+1-a)
558+
FigureCanvasAgg.draw(self)
559+
finally:
560+
self.figure.set_facecolor((r,g,b,a))
555561
ifdryrun:
556562
return
557-
# The image is "pasted" onto a white background image to safely
558-
# handle any transparency
559-
image=Image.fromarray(np.asarray(self.buffer_rgba()))
560-
background=Image.new("RGB",image.size,"white")
561-
background.paste(image,image)
562563
ifpil_kwargsisNone:
563564
pil_kwargs= {}
564565
forkin ["quality","optimize","progressive"]:
@@ -575,8 +576,9 @@ def print_jpg(self, filename_or_obj, *args, dryrun=False, pil_kwargs=None,
575576
"quality will be 75, matching the default of Pillow and "
576577
"libjpeg.")
577578
pil_kwargs.setdefault("dpi", (self.figure.dpi,self.figure.dpi))
578-
returnbackground.save(
579-
filename_or_obj,format='jpeg',**pil_kwargs)
579+
# Drop alpha channel now.
580+
return (Image.fromarray(np.asarray(self.buffer_rgba())[..., :3])
581+
.save(filename_or_obj,format='jpeg',**pil_kwargs))
580582

581583
print_jpeg=print_jpg
582584

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp