9.1 Introduction

PyX focuses on the creation of scaleable vector graphics. However, PyX also allows for the output of bitmap images. Still, the support for creation and handling of bitmap images is quite limited. On the other hand the interfaces are built that way, that its trivial to combine PyX with the ``Python Image Library'', also known as ``PIL''.

The creation of a bitmap can be performed out of some unpacked binary data by first creating image instances:

from pyx import *
image_bw = bitmap.image(2, 2, "L", "\0\377\377\0")
image_rgb = bitmap.image(3, 2, "RGB", "\77\77\77\177\177\177\277\277\277"
                                      "\377\0\0\0\377\0\0\0\377")
Now image_bw is a $2\times2$ grayscale image. The bitmap data is provided by a string, which contains two black ("\0" == chr(0)) and two white ("\377" == chr(255)) pixels. Currently the values per (colour) channel is fixed to 8 bits. The coloured image image_rgb has $3\times2$ pixels containing a row of 3 different gray values and a row of the three colours red, green, and blue.

The images can then be wrapped into bitmap instances by:

bitmap_bw = bitmap.bitmap(0, 1, image_bw, height=0.8)
bitmap_rgb = bitmap.bitmap(0, 0, image_rgb, height=0.8)
When constructing a bitmap instance you have to specify a certain position by the first two arguments fixing the bitmaps lower left corner. Some optional arguments control further properties. Since in this example there is no information about the dpi-value of the images, we have to specify at least a width or a height of the bitmap.

The bitmaps are now to be inserted into a canvas:

c = canvas.canvas()
c.insert(bitmap_bw)
c.insert(bitmap_rgb)
c.writeEPSfile("bitmap")
Figure 9.1 shows the resulting output.
Figure 9.1: An introductory bitmap example.
\includegraphics{bitmap}