Skip to content

Commit 03ecd78

Browse files
committed
Save a couple of numpy conversions.
1 parent d143c0b commit 03ecd78

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lib/matplotlib/backends/backend_agg.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,8 @@ def print_png(self, filename_or_obj, *args,
500500
"""
501501
FigureCanvasAgg.draw(self)
502502
mpl.image.imsave(
503-
filename_or_obj, np.asarray(self.buffer_rgba()), format="png",
504-
origin="upper", dpi=self.figure.dpi,
505-
metadata=metadata, pil_kwargs=pil_kwargs)
503+
filename_or_obj, self.buffer_rgba(), format="png", origin="upper",
504+
dpi=self.figure.dpi, metadata=metadata, pil_kwargs=pil_kwargs)
506505

507506
def print_to_buffer(self):
508507
FigureCanvasAgg.draw(self)

lib/matplotlib/image.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,15 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
15141514
origin = mpl.rcParams["image.origin"]
15151515
if origin == "lower":
15161516
arr = arr[::-1]
1517-
rgba = sm.to_rgba(arr, bytes=True)
1517+
if (isinstance(arr, memoryview) and arr.format == "B"
1518+
and arr.ndim == 3 and arr.shape[-1] == 4):
1519+
# Such an ``arr`` would also be handled fine by sm.to_rgba (after
1520+
# casting with asarray), but it is useful to special-case it
1521+
# because that's what backend_agg passes, and can be in fact used
1522+
# as is, saving a few operations.
1523+
rgba = arr
1524+
else:
1525+
rgba = sm.to_rgba(arr, bytes=True)
15181526
if pil_kwargs is None:
15191527
pil_kwargs = {}
15201528
pil_shape = (rgba.shape[1], rgba.shape[0])

0 commit comments

Comments
 (0)