I can: img = Image.open(content)
But how do I pass 'img' back to Django for saving?
My short class is pasted at end. The problem reported stems from the line:
name=super(CustomImageStorage,self).save(name,img)
It barfs in PIL saying:
Exception Type: AttributeError
Exception Value:
chunks
Exception Location: /usr/lib/python2.5/site-packages/PIL/Image.py in
__getattr__, line 493
I dir() the content and img objects and sure enough, img has no 'chunks'
attribute.
Anyway, I can always use img.save(path) but wanted to keep the image in memory
and not touch ground with it.
Any ideas?
\d
Code:
class CustomImageStorage( FileSystemStorage ):
def __init__(self, location, base_url, makethumbs=False,dropshadow=None):
super(CustomImageStorage,self).__init__(location=location,base_url=base_url)
self._makethumbs = makethumbs
self._dropshadow = dropshadow
def get_available_name( self, name ):
self.delete(name)
return name
def save(self, name, content ):
content.seek(0)
img = Image.open(content)
if self._dropshadow:
img = dropShadow(img, background = self._dropshadow['background'],
shadow = self._dropshadow['shadow'],
offset = self._dropshadow['offset'],
iterations = self._dropshadow['iterations'],
border = self._dropshadow['border']
)
if self._makethumbs:
try:
thumb = img.copy()
fpaf = self.thumbPath(name)
thumb.thumbnail((64,64),Image.ANTIALIAS)
thumb.save( fpaf )
except:
pass # some damn error.
img.seek(0) # Random attempt at a fix
name = super( CustomImageStorage,self).save(name,img) # was content
return name
def delete( self, name):
super(CustomImageStorage,self).delete(name)
if self._makethumbs:
# Now to remove the thumbnail too.
try:
fpaf = self.thumbPath(name)
os.unlink( fpaf )
except:
pass # Can't delete it... whatever.
def thumbPath( self, name ):
return os.path.join(self.location,'djanthumbs', name)
Thanks,
\d
This isn't going to be too helpful, directly, but I'll note that you
aren't being particularly dense for not understanding this stuff.
Customising and tweaking the file upload and internals handling is
possible, but it's not particularly simple or clearly explained,
currently. At some point we need to do another pass over the
documentation to add in a more holistic story for the fine-grained
tweaking.
Basically, hang in there and be prepared to experiment a bit and
possibly read some source code. Take notes as you go.
Regards,
Malcolm
Heh -- thanks for that! It does lift my spirits somewhat. I have been planning
to write a short tut on how to manage uploads, so I will certainly be keeping
notes.
\d
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
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
\d