[Zope] Problem with ThumbNail Image

Tom Cameron tom@cameron.to
Fri, 7 Sep 2001 08:35:28 +1000


OK the problem is most likely the way you call the images from ZODB. I am a
bit new at this, but generally python scripts see zope objects as string
objects and not files. this means that you can't do 'file' things with them
by default.

You need to use the StringIO function to allow you to open the Zope object
as if it were a file.

I spent many angry hours working this out too. In the end I came up with
this function (partly stolen from other places and partly mine). It should
help you work it out.

Tom
=====================================
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 StringIO import StringIO
  import os.path

  # create a thumbnail image file
  original_image=getattr(self, original_id)
  original_file=StringIO(str(original_image.data))
  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 + '.tn' + str(size) + '.jpg'

  # if there's and old thumbnail, delete it
  if thumbnail_id in self.objectIds():
    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')
=====================================


=> -----Original Message-----
=> From: Sylvain Boureliou [mailto:sylvain.boureliou@msg-software.com]
=> Sent: Thursday, 6 September 2001 10:22 PM
=> To: 'Tom Cameron'
=> Subject: RE: [Zope] Problem with ThumbNail Image
=>
=>
=> In fact i would like to convert on the fly jpg images in thumbnail with
=> Python script.
=> Images are in the ZODB.
=>
=> def ThumbNail(self, image):
=>     import Image, os, sys
=>     texte = "ok"
=>     img = Image.open(image) --------->>> ERROR !!
=>     img.thumbnail((50, 50))
=>     img.save('essaiThumbnail.jpg', "JPEG")
=>     return img
=>
=> It doesn't work !
=> It works if the jpg image is in the file system and if i use the
=> script from
=> the Python command line !!!.
=>
=>
=>
=>
=> -----Message d'origine-----
=> De : Tom Cameron [mailto:tom@cameron.to]
=> Envoyé : jeudi 6 septembre 2001 06:31
=> À : sylvain.boureliou@msg-software.com
=> Objet : RE: [Zope] Problem with ThumbNail Image
=>
=>
=> Clarify where the image is. do you mean the OS file system or in
=> the ZODB?
=>
=> Tom
=>
=> => -----Original Message-----
=> => From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of
=> => Sylvain Boureliou
=> => Sent: Tuesday, 4 September 2001 11:51 PM
=> => To: zope@zope.org
=> => Subject: [Zope] Problem with ThumbNail Image
=> =>
=> =>
=> => Hi all
=> =>
=> => I would like to create a thumbnail from a jpeg file.
=> => Here is the fonction i use :
=> =>
=> => def ThumbNail(self):
=> =>     import Image, os, sys
=> =>     texte = "ok"
=> =>     img = Image.open("essai.jpg")
=> =>     img.thumbnail((50, 50))
=> =>     img.save('essaiThumbnail.jpg', "JPEG")
=> =>     return texte
=> =>
=> => The file "essai.jpg" is in the Zope directory.
=> => It works very well from the Python command line, but it doesn't
=> => work when i
=> => use it in an external method.
=> => Zope finds the file but this error is raised :
=> =>
=> => (i'm using Zope 2.4 win32 - Python 2.1 - PIL 1.1.1)
=> => Error Type: IOError
=> => Error Value: cannot identify image file
=> => ....
=> => File C:\Program Files\zope\Extensions\ScriptsEpinet.py, line 24, in
=> => ThumbNail
=> =>   File c:\python21\pil\Image.py, line 902, in open
=> => IOError: (see above)
=> =>
=> =>
=> => Thanks very much for help.
=> =>
=> => Sylvain
=> =>
=> =>
=> => _______________________________________________
=> => Zope maillist  -  Zope@zope.org
=> => http://lists.zope.org/mailman/listinfo/zope
=> => **   No cross posts or HTML encoding!  **
=> => (Related lists -
=> =>  http://lists.zope.org/mailman/listinfo/zope-announce
=> =>  http://lists.zope.org/mailman/listinfo/zope-dev )
=> =>
=>
=>