[Zope] Re: Generating thumbnails on the fly using PIL

Tino Wildenhain tino@wildenhain.de
Wed, 28 Nov 2001 11:58:27 +0100


Hi,

--On Tuesday, November 27, 2001 21:45:49 +0100 Pieter Claerhout 
<Pieter_Claerhout@CreoScitex.com> wrote:

> Hi all,
>
> I got quite a lot further now. Here's the script I have so far:
>
> import PIL.Image
> import os.path
> import cStringIO
>
> def MakeThumb(self, image, width, height):
>     oriImage = getattr(self, image)
>     oriFile = cStringIO.StringIO(oriImage.data)
>     image = PIL.Image.open(oriFile)
>     image.thumbnail((width, height))
>     thumbnailImage = StringIO()
>     image.save(thumbnailImage, "JPEG")
>     thumbnailImage.seek(0)
>     return thumbnailImage.getvalue()
>
> this all works find if you use it as:
>
> <dtml-var "PHTO_MakeThumb('AB_action_inbox.jpg', 160, 120)">
>
> As soon as you want to do something like:
>
> <dtml-var "PHTO_MakeThumb('/images/AB_action_inbox.jpg', 160, 120)">
>
> you get an AttributeError. Does anybody has a way around this?

Try it with images.PHTO_MakeThumb('AB_action_inbox.jpg',160,120)

if you use getattr, you ask your context about '/images/AB_action_inbox.jpg'
but your context does not have an attribute with this name, it has
an attribute (or acquires an attribute) 'images' which is another object
which has the attribute 'AB_action_inbox.jpg'

Mayby its even better, if you make a Product, derive your zclass from
Image and make it a method of your new image class, then you can even refer
to it like this:
<dtml-var expr="images.AB_action_inbox.PHTO_MakeThumb()">
(only if you dont name your images with extension. This yould
make it more complicated:
<dtml-var expr="images['AB_action_inbox'].PHTO_MakeThumb()">

HTH
Tino Wildenhain

HTH
Tino Wildenhain