-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathagg_buffer.py
More file actions
42 lines (30 loc) · 923 Bytes
/
agg_buffer.py
File metadata and controls
42 lines (30 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
==========
Agg Buffer
==========
Use backend agg to access the figure canvas as an RGB string and then
convert it to an array and pass it to Pillow for rendering.
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg
try:
from PIL import Image
except ImportError:
raise SystemExit("Pillow must be installed to run this example")
plt.plot([1, 2, 3])
canvas = plt.get_current_fig_manager().canvas
agg = canvas.switch_backends(FigureCanvasAgg)
agg.draw()
s = agg.tostring_rgb()
# get the width and the height to resize the matrix
l, b, w, h = agg.figure.bbox.bounds
w, h = int(w), int(h)
X = np.fromstring(s, np.uint8).reshape((h, w, 3))
try:
im = Image.fromstring("RGB", (w, h), s)
except Exception:
im = Image.frombytes("RGB", (w, h), s)
# Uncomment this line to display the image using ImageMagick's
# `display` tool.
# im.show()