I am trying to read a jpg from file and display it.<br>I am using Python with pyCairo.<br><br>I have the following code that works with some help from PIL, but it is slow.<br>I feel that there must be a more direct route.<br>
<br>There is an example at this link: <a href="http://cairographics.org/pythoncairopil/">http://cairographics.org/pythoncairopil/</a><br>but it does not work.<br><br><br>img = Image.open('image.jpg')<br><br># The next 3 lines are to get the data to pass to Image.frombuffer<br>
# I need to convert rgb to bgra.<br>l = [struct.pack('BBBB', b, g, r, 0xFF) for (r,g,b) in img.getdata()]<br><br># runs over whole image to convert to string<br>s = string.join(l, '')<br><br># runs over whole image again to get a writable buffer<br>
data = array.array('B', s)<br><br>im1 = Image.frombuffer("RGBA", img.size, data, "raw", "RGBA", 0, 1)<br><br># runs over whole image twice<br>self.img = array.array('B', im1.tostring())<br>
<br>stride = cairo.ImageSurface.format_stride_for_width(cairo.FORMAT_ARGB32, self.width())<br>self.img_surface = cairo.ImageSurface.create_for_data (self.img, cairo.FORMAT_ARGB32, self.width(), self.height(), stride)<br><br>
Yikes that is bad.<br><br>Once I have the surface, then I do<br>ctx.set_source_surface(surface, self.x, self.y)<br>make_rectangle_path(ctx, self.x, self.y, self.width(), self.height())<br>ctx.fill()<br><br>Help!<br>