[Zope] Using PIL in zope product

Tino Wildenhain tino at wildenhain.de
Tue Dec 28 03:05:43 EST 2004


Hi,

Am Montag, den 27.12.2004, 23:36 -0800 schrieb John Schinnerer:
> Hello,
> 
> I am developing an art-gallery type of product, with images of artworks
> stored as zope Image objects within a folderish product instance.
> 
> When a new gallery item instance is created, or when the Image object
> in an existing instance is changed, I want to auto-generate a thumbnail
> of the image (using PIL) and store it also as a zope Image object.
> 
> In manage_addAction for example I have this:
> ...
> imgfile = REQUEST.image_file
> 	if imgfile and imgfile.filename != '':
> 		newObjId.manage_addImage('art_piece', imgfile, newObjId.title)
> 		newObjId.manage_addImage('art_piece_thumb', makeThumb(imgfile),
> newObjId.title) 
> ...
> 
> and here is makeThumb (using PIL):
> 
> def makeThumb(imagefile):
> 	"create thumbnail image from full-size art_piece image"
> 	img = Image.open(imagefile)
> 	thumb = img.thumbnail((111,111), Image.ANTIALIAS)
> 	return thumb
> 
> I get a zope error triggered by the line
> 
> img = Image.open(imagefile)
> 
> like this:
> 
> Error Type: AttributeError
> Error Value: FileUpload instance has no attribute '__getitem__'
> (traceback further below if helpful)
> 
> So apparently there is some problem with trying to use the image file
> passed in the request with the PIL functions...it's not a "real" file
> or at least doesn't have some attributes that PIL is expecting to find,
> or something like that.
> 
> Is there a simple way around this the way I'm trying to do it?

Well, the image Object does not get created with a real file
everytime. Look at the documentation for Image.open() for this.
I use this approach in my Images update_data:
    
def update_data(self,data,content_type=None, size=None):
    OFS.Image.Image.update_data(self,data,
                              content_type=content_type,size=size)
    parser=PIL.ImageFile.Parser()
    d=self.data
    if d:
        if type(d)==type(""):
            parser.feed(d)
        else:
            while d:
                parser.feed(d.data)
                d=d.next
        parser.close()
        if parser.finished:
            img=parser.image

at this point you can use img.thumbnail() to create the miniature



More information about the Zope mailing list