File: to_numeric.py

package info (click to toggle)
matplotlib 0.99.3-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 33,060 kB
  • ctags: 28,162
  • sloc: python: 79,063; cpp: 64,496; objc: 4,513; ansic: 1,948; makefile: 146; sh: 7
file content (33 lines) | stat: -rw-r--r-- 705 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python
"""
Use backend agg to access the figure canvas as an RGB string and then
convert it to an array and pass the string it to PIL for
rendering
"""

from pylab import *
from matplotlib.backends.backend_agg import FigureCanvasAgg
try:
    import Image
except ImportError, exc:
    raise SystemExit("PIL must be installed to run this example")

plot([1,2,3])

canvas = 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 = fromstring(s, uint8)
X.shape = h, w, 3

im = Image.fromstring( "RGB", (w,h), s)
im.show()