[cairo] Trouble displaying a jpeg image

Jeffrey Britton jeffb0098 at gmail.com
Thu Jan 5 18:35:51 PST 2012

I am trying to read a jpg from file and display it.
I am using Python with pyCairo.

I have the following code that works with some help from PIL, but it is
I feel that there must be a more direct route.

There is an example at this link:  http://cairographics.org/pythoncairopil/
but it does not work.

img = Image.open('image.jpg')

# The next 3 lines are to get the data to pass to Image.frombuffer
# I need to convert rgb to bgra.
l = [struct.pack('BBBB', b, g, r, 0xFF) for (r,g,b) in img.getdata()]

# runs over whole image to convert to string
s = string.join(l, '')

# runs over whole image again to get a writable buffer
data = array.array('B', s)

im1 = Image.frombuffer("RGBA", img.size, data, "raw", "RGBA", 0, 1)

# runs over whole image twice
self.img = array.array('B', im1.tostring())

stride = cairo.ImageSurface.format_stride_for_width(cairo.FORMAT_ARGB32,
self.img_surface = cairo.ImageSurface.create_for_data (self.img,
cairo.FORMAT_ARGB32, self.width(), self.height(), stride)

Yikes that is bad.

Once I have the surface, then I do
ctx.set_source_surface(surface, self.x, self.y)
make_rectangle_path(ctx, self.x, self.y, self.width(), self.height())

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20120105/0b6018f2/attachment.html>

More information about the cairo mailing list