Re: FileSystemStorage content vs PIL Image - **Initial solution**

2009-01-05 Thread Donn

On Monday, 05 January 2009 17:50:09 A Melé wrote:
> Donn, take a look at django-thumbs source code
Many thanks -- it looks familiar now :)

\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
-~--~~~~--~~--~--~---



Re: FileSystemStorage content vs PIL Image - **Initial solution**

2009-01-05 Thread A Melé

Donn, take a look at django-thumbs source code, it integrates PIL and
SotrageBackend: 
http://code.google.com/p/django-thumbs/source/browse/trunk/thumbs.py
I hope the code helps you.


Regards,

Antonio Melé
http://django.es/blog/


On 5 ene, 13:30, Donn  wrote:
> On Monday, 05 January 2009 14:00:16 Eric Abrahamsen wrote:
>
> > content.write(o.getvalue())
> > super(CustomImageStorage,self).save(name, content)
>
> That gives:
> 'InMemoryUploadedFile' object has no attribute '_mode'
> Exception
> Location:       /usr/lib/python2.5/site-packages/django/core/files/base.py in
> _get_mode, line 39
>
> I recall getting this mode error and went past it to my eventual solution. But
> I'd like to know if it's kosher or crazy.
>
> \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
-~--~~~~--~~--~--~---



Re: FileSystemStorage content vs PIL Image - **Initial solution**

2009-01-05 Thread Donn

On Monday, 05 January 2009 14:00:16 Eric Abrahamsen wrote:
> content.write(o.getvalue())
> super(CustomImageStorage,self).save(name, content)

That gives:
'InMemoryUploadedFile' object has no attribute '_mode'
Exception 
Location:   /usr/lib/python2.5/site-packages/django/core/files/base.py in 
_get_mode, line 39

I recall getting this mode error and went past it to my eventual solution. But 
I'd like to know if it's kosher or crazy.

\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
-~--~~~~--~~--~--~---



Re: FileSystemStorage content vs PIL Image - **Initial solution**

2009-01-05 Thread Eric Abrahamsen


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
-~--~~~~--~~--~--~---



Re: FileSystemStorage content vs PIL Image - **Initial solution**

2009-01-05 Thread Donn

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()) 

# 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
-~--~~~~--~~--~--~---