PIL: convert an image-object back to file object
Hi, I use an external method utilizing PIL (python image library) to do some stuff with an image. As I want to do the changes on-the-fly, I pass the fileUpload instance from my HTML-form to a python script. The script calls a external method. The external method uses PIL to convert the fielUpload instance to a PIL.Image.Image instance, does some stuff and returns the PIL.Image.Image instance to the python script. In the script I want to save the returned object as an Zope image object using manage_addImage(). But the manage_addImage method expects a file object, not a PIL.Image.Image. Here’s the traceback Traceback (innermost last): • Module ZPublisher.Publish, line 98, in publish • Module ZPublisher.mapply, line 88, in mapply • Module ZPublisher.Publish, line 39, in call_object • Module Shared.DC.Scripts.Bindings, line 252, in __call__ • Module Shared.DC.Scripts.Bindings, line 283, in _bindAndExec • Module Products.PythonScripts.PythonScript, line 314, in _exec • Module Script (Python), line 13, in check <PythonScript at /test/autoimage/check> Line 13 • Module OFS.Image, line 589, in manage_addImage • Module OFS.Image, line 434, in manage_upload • Module OFS.Image, line 469, in _read_data AttributeError: Image instance has no attribute 'read' How do I convert the image object back to a file object so I can add it to Zope? Regards Christoph
Christoph Landwehr wrote at 2003-11-13 19:33 +0100:
I use an external method utilizing PIL (python image library) to do some stuff with an image.
As I want to do the changes on-the-fly, I pass the fileUpload instance from my HTML-form to a python script. The script calls a external method. The external method uses PIL to convert the fielUpload instance to a PIL.Image.Image instance, does some stuff and returns the PIL.Image.Image instance to the python script.
In the script I want to save the returned object as an Zope image object using manage_addImage(). But the manage_addImage method expects a file object, not a PIL.Image.Image.
It will also allow a string representing the files content... -- Dieter
As I want to do the changes on-the-fly, I pass the fileUpload instance from my HTML-form to a python script. The script calls a external method. The external method uses PIL to convert the fielUpload instance to a PIL.Image.Image instance, does some stuff and returns the PIL.Image.Image instance to the python script.
In the script I want to save the returned object as an Zope image object using manage_addImage(). But the manage_addImage method expects a file object, not a PIL.Image.Image.
Save the PIL Image into an intermediary cStringIO instance and pass that one into manage_addImage. jens
Thanx Jens and Dieter for the hint that manage_addImage allows string input. Jens Vagelpohl schrieb:
In the script I want to save the returned object as an Zope image object using manage_addImage(). But the manage_addImage method expects a file object, not a PIL.Image.Image.
Save the PIL Image into an intermediary cStringIO instance and pass that one into manage_addImage.
Somehow I seem to pass the wong data to StringIO: image=StringIO(my_PIL_image.tostring()) returns me a StringIO instance of the Object. However I don't get valid image data: self.manage_addImage('foo', image, title='bar', content_type='image/jpeg') adds an object to Zope with about the right filesize but I can not viw the image: view returns the string 'bar' Is the PIL Image method tostring() the right choice? Regards Christoph
Christoph Landwehr schrieb:
Somehow I seem to pass the wong data to StringIO:
image=StringIO(my_PIL_image.tostring()) Is the PIL Image method tostring() the right choice?
I found the solution (thanx to Fredrik Lundh) : file = StringIO() image.save(file, "JPEG") return file.getvalue() Regards Christoph
participants (3)
-
Christoph Landwehr -
Dieter Maurer -
Jens Vagelpohl