Re: Generating thumbnails on the fly using PIL
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? Cheers, Pieter
on or about, Tuesday, November 27, 2001, we have reason to believe that Pieter Claerhout wrote something along the lines of : PC> Hi all, [snip] PC> this all works find if you use it as: PC> <dtml-var "PHTO_MakeThumb('AB_action_inbox.jpg', 160, 120)"> PC> As soon as you want to do something like: PC> <dtml-var "PHTO_MakeThumb('/images/AB_action_inbox.jpg', 160, 120)"> PC> you get an AttributeError. Does anybody has a way around this? PC> Cheers, Cheers to you to , Pieter . You cannot do traditional directory addressing with slashes in dtml. try something like : <dtml-with images> <dtml-var "PHTO_MakeThumb('AB_action_inbox.jpg', 160, 120)"> </dtml-with> good luck :-) -- Geir Bækholt web-developer geirh@funcom.com funcom oslo | webdev-team
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
Pieter Claerhout writes:
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? Whenever I see such a question (which I do rather (too) often ;-)), I reply: use "restrictedTraverse"....
Dieter
participants (4)
-
Dieter Maurer -
Geir B�kholt -
Pieter Claerhout -
Tino Wildenhain