I struggled a bit to get this working today, but eventually found a
solution. I am trying to write out JPEG images stored in the
datastore. (They are all JPEGs to begin with.)

Initially I was trying a number of things, such as dumping the JPEG
blog directly into drawImage, running the blob through the Google
Image library, wrapping the JPEG blob in a StringIO file, etc - but
either AppEngine would either completely crash, or I would get errors
like:

AttributeError: 'cStringIO.StringO' object has no attribute 'rfind'

or

IOError: Cannot open resource.

or

RuntimeError: Imaging Library not available, unable to import bitmaps
only jpegs fileName=<StringIO.StringIO instance at 0x1849d78>

The solution was to create an extension of the ImageReader class
called JpegBlobImageReader like so:

from StringIO import StringIO
from google.appengine.api import images

class JpegBlobImageReader(pdflibutils.ImageReader):

  def __init__(self, blob):

    self.fileName = "JPEGBLOB_%d" % id(self)

    self.fp = StringIO()
    self.fp.write(blob)

    self._image = blob
    self.jpeg_fh = self._jpeg_fh
    self._data = blob
    self._dataA = None

    image = images.Image(blob)
    self._width = image.width
    self._height = image.height
    self._transparent = False

You can then add an image like this:

canvas.drawImage(JpegBlobImageReader(line.item.drawing), 9*cm, height
- 10*cm, 8*cm, 8*cm)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to