On Jan 5, 2009, at 7:47 PM, Donn wrote:

>
> On Monday, 05 January 2009 00:51:03 Donn wrote:
>> I am stuck on the interface between a PIL Image and a
>> FilesystemStorage 'content' object.
>>
>> I can: img = Image.open(content)
>> But how do I pass 'img' back to Django for saving?
>
> I have some kind of working example now.  I won't say I savvy it  
> properly, but
> it's a case of finding objects that FileSystemStorage.save will  
> accept.
>
> This object needs a 'chunks' attribute. It's raw data must also be  
> in binary
> format. So, I need to go from a PIL 'img' out to this magical object:
>
> You need this class
> from django.core.files.base import ContentFile
>
> And this:
> try:
> from cStringIO import StringIO
> except ImportError:
> from StringIO import StringIO
>
> In the code, insert:
>
> #Make a thing to hold our data
> o = StringIO()
>
> #convert the image to a binary file-like memory object
> img.save(o,'PNG')
>
> # We NEED this object because it has 'chunks'
> content = ContentFile(o.getvalue())

Instead of instantiating a new object, have you tried writing back to  
the original "content" file object that was passed in as a parameter  
to save? It's got a write method:

content.write(o.getvalue())
super(CustomImageStorage,self).save(name, content)

That might avoid causing new problems...

E


>
>
> # Now the content is accepted.
> # CutsomImageStorage is my FileSystemStorage subclass, see OP.
> super( CustomImageStorage,self).save(name, content)
>
> HTH; any corrections for sheer stupidity? :)
>
> \d
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to