Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
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...).
  • Loading branch information
anntzer committed Aug 19, 2018
commit 54cd9ceeac50d2a986c86a1c98cc67113dc73381
7 changes: 7 additions & 0 deletions doc/api/next_api_changes/2018-07-22-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
`FigureCanvasAgg.buffer_rgba` and `RendererAgg.buffer_rgba` now 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.
2 changes: 1 addition & 1 deletion examples/misc/agg_buffer_to_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
fig.canvas.draw()

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wowzers. Good catch.


# now display the array X as an Axes in a new figure
fig2 = plt.figure()
Expand Down
17 changes: 8 additions & 9 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def tostring_argb(self):
return self._renderer.tostring_argb()

def buffer_rgba(self):
return self._renderer.buffer_rgba()
return memoryview(self._renderer)

def clear(self):
self._renderer.clear()
Expand Down Expand Up @@ -421,40 +421,39 @@ def get_renderer(self, cleared=False):
return self.renderer

def tostring_rgb(self):
'''Get the image as an RGB byte string.
"""Get the image as an RGB byte string.

`draw` must be called at least once before this function will work and
to update the renderer for any subsequent changes to the Figure.

Returns
-------
bytes
'''
"""
return self.renderer.tostring_rgb()

def tostring_argb(self):
'''Get the image as an ARGB byte string
"""Get the image as an ARGB byte string.

`draw` must be called at least once before this function will work and
to update the renderer for any subsequent changes to the Figure.

Returns
-------
bytes

'''
"""
return self.renderer.tostring_argb()

def buffer_rgba(self):
'''Get the image as an RGBA byte string.
"""Get the image as a memoryview to the renderer's buffer.

`draw` must be called at least once before this function will work and
to update the renderer for any subsequent changes to the Figure.

Returns
-------
bytes
'''
memoryview
"""
return self.renderer.buffer_rgba()

def print_raw(self, filename_or_obj, *args, **kwargs):
Expand Down