[Zope] PIL, StringIO, manage_delObject and thumbnails

Tom Cameron tom@mooball.com
Sun, 29 Jul 2001 11:48:12 +1000


I have been trying for several days now to create thumbnails in Zope. I
ended up looking at the example code at:
http://www.zope.org/Members/michel/ZB/ScriptingZope.html

I am not sure how current this document is, but I tried to use the thumbnail
script with no success.

After many frustrating hours I found the following changes worked:

      def makeThumbnail(self, original_id, size=128):
          """
          Makes a thumbnail image given an image Id when called on a Zope
          folder.

          The thumbnail is a Zope image object that is a small JPG
          representation of the original image. The thumbnail has a
          'original_id' property set to the id of the full size image
          object.
          """

          from PIL import Image
--        from cStringIO import StringIO
++        from StringIO import StringIO    <=I cant seem to get cStringIO to
work
          import os.path

          # create a thumbnail image file
          original_image=getattr(self, original_id)
--        original_file=StringIO(original_image.data)
++        original_file=StringIO(str(original_image.data)) <=needed to
ensure string
          image=Image.open(original_file)
          image=image.convert('RGB')
          image.thumbnail((size,size))
          thumbnail_file=StringIO()
          image.save(thumbnail_file, "JPEG")
          thumbnail_file.seek(0)

          # create an id for the thumbnail
          path, ext=os.path.splitext(original_id)
          thumbnail_id=path + '.thumb.jpg'

          # if there's and old thumbnail, delete it
          if thumbnail_id in self.objectIds():
--            self.manage_delObject(thumbnail_id)      <= this does not work
++            self.manage_delObjects([thumbnail_id])

          # create the Zope image object
          self.manage_addProduct['OFSP'].manage_addImage(thumbnail_id,
                                                         thumbnail_file,
                                                         'thumbnail image')
          thumbnail_image=getattr(self, thumbnail_id)

          # set the 'originial_id' property
          thumbnail_image.manage_addProperty('original_id', original_id,
'string')


I have no idea if these are errors or if things have changed. I am using
Zope 2.3.2 and Python 1.5.2 on FreeBSD 4.0.

I just thought I should report what I have found and hopefully save someone
else the pain

Tom