Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 54cd9ce

Browse files
committed
Change {FigureCanvasAgg,RendererAgg}.buffer_rgba to return a memoryview.
The `buffer_rgba` method now allows direct access to the renderer's underlying buffer (as a `(m, n, 4)`-shape memoryview) rather than copying the data to a new bytestring. This is consistent with the behavior on Py2, where a buffer object was returned. While this is technically a backwards-incompatible change, memoryviews are in fact quite compatible with bytes for most uses, and I'd argue that the bigger compatibility break was the change from no-copy in Py2 to copy in Py3 (after all that's the main point of the method...).
1 parent f1b64ad commit 54cd9ce

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
`FigureCanvasAgg.buffer_rgba` and `RendererAgg.buffer_rgba` now return a memoryview
2+
```````````````````````````````````````````````````````````````````````````````````
3+
4+
The ``buffer_rgba`` method now allows direct access to the renderer's
5+
underlying buffer (as a ``(m, n, 4)``-shape memoryview) rather than copying the
6+
data to a new bytestring. This is consistent with the behavior on Py2, where a
7+
buffer object was returned.

examples/misc/agg_buffer_to_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
fig.canvas.draw()
1616

1717
# grab the pixel buffer and dump it into a numpy array
18-
X = np.array(fig.canvas.renderer._renderer)
18+
X = np.array(fig.canvas.renderer.buffer_rgba())
1919

2020
# now display the array X as an Axes in a new figure
2121
fig2 = plt.figure()

lib/matplotlib/backends/backend_agg.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def tostring_argb(self):
272272
return self._renderer.tostring_argb()
273273

274274
def buffer_rgba(self):
275-
return self._renderer.buffer_rgba()
275+
return memoryview(self._renderer)
276276

277277
def clear(self):
278278
self._renderer.clear()
@@ -421,40 +421,39 @@ def get_renderer(self, cleared=False):
421421
return self.renderer
422422

423423
def tostring_rgb(self):
424-
'''Get the image as an RGB byte string.
424+
"""Get the image as an RGB byte string.
425425
426426
`draw` must be called at least once before this function will work and
427427
to update the renderer for any subsequent changes to the Figure.
428428
429429
Returns
430430
-------
431431
bytes
432-
'''
432+
"""
433433
return self.renderer.tostring_rgb()
434434

435435
def tostring_argb(self):
436-
'''Get the image as an ARGB byte string
436+
"""Get the image as an ARGB byte string.
437437
438438
`draw` must be called at least once before this function will work and
439439
to update the renderer for any subsequent changes to the Figure.
440440
441441
Returns
442442
-------
443443
bytes
444-
445-
'''
444+
"""
446445
return self.renderer.tostring_argb()
447446

448447
def buffer_rgba(self):
449-
'''Get the image as an RGBA byte string.
448+
"""Get the image as a memoryview to the renderer's buffer.
450449
451450
`draw` must be called at least once before this function will work and
452451
to update the renderer for any subsequent changes to the Figure.
453452
454453
Returns
455454
-------
456-
bytes
457-
'''
455+
memoryview
456+
"""
458457
return self.renderer.buffer_rgba()
459458

460459
def print_raw(self, filename_or_obj, *args, **kwargs):

0 commit comments

Comments
 (0)