You are reading an old version of the documentation (v1.2.1). For the latest version see

This Page

pylab_examples example code: to_numeric.pyΒΆ

(Source code, png, hires.png, pdf)

#!/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

import pylab
from matplotlib.backends.backend_agg import FigureCanvasAgg

    from PIL import Image
except ImportError:
    raise SystemExit("PIL must be installed to run this example")


canvas = pylab.get_current_fig_manager().canvas

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

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

Keywords: python, matplotlib, pylab, example, codex (see Search examples)