[Zope] Resizing an Image with PIL via a Form Upload

2007-02-22 Thread Tom Von Lahndorff


I'm trying to resize an image uploaded from a form using PIL. I got it 
to work on an existing image but I can't seem to get it to work on an 
image that's uploaded through the form. The ID of the file I'm trying to 
upload is DSCF0004.jpg. The form, script and traceback are listed 
below. Thanks in advance.


form action=processed/imgSizer method=post 
enctype=multipart.form-data
input type=file name=original_id input type=submit 
name=submit value= Add 

/form

(imgSizer External Method)
1 def makeImages(self, original_id):
2 
3import PIL.Image

4import PIL
5from StringIO import StringIO
6import os.path
7
8original_image=getattr(self, original_id)
9original_file=StringIO(str(original_image.data))
...

returns:

Traceback (innermost last):

   * Module ZPublisher.Publish, line 115, in publish
   * Module ZPublisher.mapply, line 88, in mapply
   * Module ZPublisher.Publish, line 41, in call_object
   * Module Products.ExternalMethod.ExternalMethod, line 225, in __call__
 __traceback_info__: ((Folder at /dev/imgtest/processed,
 'DSCF0004.jpg'), {}, None)
   * Module .../Zope/Extensions/imgSizer.py, line 9, in makeImages

AttributeError: DSCF0004.jpg

___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Resizing an Image with PIL via a Form Upload

2007-02-22 Thread Marco Bizzarri

Jusst a shot in the dark: can you try to use a name without a dot inside?
--
Marco Bizzarri
http://iliveinpisa.blogspot.com/
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Resizing an Image with PIL via a Form Upload

2007-02-22 Thread Jonathan


- Original Message - 
From: Tom Von Lahndorff [EMAIL PROTECTED]

To: zope@zope.org
Sent: Thursday, February 22, 2007 1:29 PM
Subject: [Zope] Resizing an Image with PIL via a Form Upload




I'm trying to resize an image uploaded from a form using PIL. I got it to 
work on an existing image but I can't seem to get it to work on an image 
that's uploaded through the form. The ID of the file I'm trying to upload 
is DSCF0004.jpg. The form, script and traceback are listed below. Thanks 
in advance.


form action=processed/imgSizer method=post 
enctype=multipart.form-data
input type=file name=original_id input type=submit name=submit 
value= Add 

/form

(imgSizer External Method)
1 def makeImages(self, original_id):
2 3import PIL.Image
4import PIL
5from StringIO import StringIO
6import os.path
7
8original_image=getattr(self, original_id)
9original_file=StringIO(str(original_image.data))


just a guess, but i don't think getattr is returning what you think it is 
returning.


The image has not been 'stored' anywhere yet, it is just a FileUpload 
instance.  To get at the information in this instance:


# the var name used in your form is misleading, it is not just the image id
fileName = self.REQUEST['original_id'].filename
fileData = self.REQUEST['original_id'].read()


hth

Jonathan 


___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Resizing an Image with PIL via a Form Upload

2007-02-22 Thread Andrew Langmead

On Feb 22, 2007, at 1:29 PM, Tom Von Lahndorff wrote:


8original_image=getattr(self, original_id)
9original_file=StringIO(str(original_image.data))

[and later]

AttributeError: DSCF0004.jpg


I think your line numbers are off by one, because I'd think it is the  
getattr that would be causing the Attribute Error (at most, line 9  
could give an attribute error for something called data.


Zope turns file upload objects into   
ZPublisher.HTTPRequest.FileUpload instances. and pass them to your  
method. FileUpload instances have an interface similar to python file  
objects, you can .read(), .seek(), .close() or do whatever you want  
to them and might not need to wrap the contents into a StringIO  
object first. (your trying to fetch the file as an attribute of  
self seems to imply that you think the form data is already  
uploaded into zope and is an Image object in a folder. It isn't there  
yet, and won't be unless you store it there.)


I think if you removed lines 8 and 9 (by your numbering) and renamed  
the second argument to the method to original_file, things would  
work as you need:


1 def makeImages(self, original_file):
2
3import PIL.Image
4import PIL
5import os.path
6
...





___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Resizing an Image with PIL via a Form Upload

2007-02-22 Thread Tom Von Lahndorff
Thanks for everyone's help. I got it to work, here's the script. It  
creates 4 images, enhances them and adds some properties to the  
largest one.


--

def makeImages(self, imagefile, newkeywords, newcaption):

import PIL.Image, ImageEnhance, ImageFilter
import PIL
from StringIO import StringIO
import os.path
import datetime
import time

# create the data in a new PIL Image.
image=PIL.Image.open(imagefile)
image=image.convert('RGB')
image=image.filter(ImageFilter.SHARPEN)
image=image.resize((640, 480), PIL.Image.ANTIALIAS)
image2=image.resize((320, 240), PIL.Image.ANTIALIAS)
image3=image.resize((160, 120), PIL.Image.ANTIALIAS)
image4=image.resize((80, 60), PIL.Image.ANTIALIAS)

# get the data in memory.
newimage_file=StringIO()
image.save(newimage_file, JPEG)
newimage_file.seek(0)
newimage_file2=StringIO()
image2.save(newimage_file2, JPEG)
newimage_file2.seek(0)
newimage_file3=StringIO()
image3.save(newimage_file3, JPEG)
newimage_file3.seek(0)
newimage_file4=StringIO()
image4.save(newimage_file4, JPEG)
newimage_file4.seek(0)

# create an id for the image
now = datetime.datetime.now()
newimageid=now.strftime('%Y%m%d%H%M%S')
newimage_id=newimageid + '-640.jpg'
newimage_id2=newimageid + '-320.jpg'
newimage_id3=newimageid + '-160.jpg'
newimage_id4=newimageid + '-80.jpg'

# if there's an old image, delete it
if newimage_id in self.objectIds():
self.manage_delObjects([newimage_id])
if newimage_id2 in self.objectIds():
self.manage_delObjects([newimage_id2])
if newimage_id3 in self.objectIds():
self.manage_delObjects([newimage_id3])
if newimage_id4 in self.objectIds():
self.manage_delObjects([newimage_id4])

# create the Zope image object for the new image
self.manage_addProduct['OFSP'].manage_addImage(newimage_id,  
newimage_file, '')
self.manage_addProduct['OFSP'].manage_addImage(newimage_id2,  
newimage_file2, '')
self.manage_addProduct['OFSP'].manage_addImage(newimage_id3,  
newimage_file3, '')
self.manage_addProduct['OFSP'].manage_addImage(newimage_id4,  
newimage_file4, '')


# now find the new zope object so we can modify
# its properties.
newimage_image=getattr(self, newimage_id)
newimage_image.manage_addProperty('keywords', '', 'string')
newimage_image.manage_addProperty('caption', '', 'string')
newimage_image.manage_addProperty('imageid', '', 'string')
newimage_image.manage_changeProperties(keywords=newkeywords,  
caption=newcaption, imageid=newimageid)

newimage_image2=getattr(self, newimage_id2)
newimage_image2.manage_addProperty('imagefile2', '', 'string')
newimage_image3=getattr(self, newimage_id3)
newimage_image3.manage_addProperty('imagefile3', '', 'string')
newimage_image4=getattr(self, newimage_id4)
newimage_image4.manage_addProperty('imagefile4', '', 'string')

--

On Feb 22, 2007, at 4:11 PM, Andrew Langmead wrote:


On Feb 22, 2007, at 1:29 PM, Tom Von Lahndorff wrote:


8original_image=getattr(self, original_id)
9original_file=StringIO(str(original_image.data))

[and later]

AttributeError: DSCF0004.jpg


I think your line numbers are off by one, because I'd think it is  
the getattr that would be causing the Attribute Error (at most,  
line 9 could give an attribute error for something called data.


Zope turns file upload objects into   
ZPublisher.HTTPRequest.FileUpload instances. and pass them to your  
method. FileUpload instances have an interface similar to python  
file objects, you can .read(), .seek(), .close() or do whatever you  
want to them and might not need to wrap the contents into a  
StringIO object first. (your trying to fetch the file as an  
attribute of self seems to imply that you think the form data is  
already uploaded into zope and is an Image object in a folder. It  
isn't there yet, and won't be unless you store it there.)


I think if you removed lines 8 and 9 (by your numbering) and  
renamed the second argument to the method to original_file,  
things would work as you need:


1 def makeImages(self, original_file):
2
3import PIL.Image
4import PIL
5import os.path
6
...





___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )