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(&#39;image.jpg&#39;)<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(&#39;BBBB&#39;, 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, &#39;&#39;)<br><br># runs over whole image again to get a writable buffer<br>
data = array.array(&#39;B&#39;, s)<br><br>im1 = Image.frombuffer(&quot;RGBA&quot;, img.size, data, &quot;raw&quot;, &quot;RGBA&quot;, 0, 1)<br><br># runs over whole image twice<br>self.img = array.array(&#39;B&#39;, 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>