I setup S3Storage as my default storage and can successfully upload
images to S3 without overriding save on my profile_update below. Right
now, when I submit a form with an image I get the original image
uploaded in my S3 bucket not a thumbnail and an IO Error - "cannot
identify image file" - traceback all the way down. I know its
complicated but I have to imagine other people will attempt to create
a thumbnail in memory and upload to S3 without saving locally. Thank
you for helping.

models.py
========
class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    picture = models.ImageField(upload_to='profiles', null=True,
blank=True)

views.py
=======
def resize_image(buf, size=(100, 100)):
    import Image as PILImage
    import cStringIO
    f = cStringIO.StringIO(buf)
    image = PILImage.open(f)
    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')
    image.thumbnail(size, PILImage.ANTIALIAS)
    o = cStringIO.StringIO()
    image.save(o, "JPEG")
    return o.getvalue()

@login_required(redirect_field_name='redirect_to')
def profile_update(request):
    profile = request.user.get_profile()
    ...

    if request.method == 'POST':
         pform = ProfileForm(request.POST, request.FILES,
instance=profile)
    else:
         pform = ProfileForm(instance=profile)

    if pform.is_valid()
        new_profile = pform.save(commit=False)
        if(pform.cleaned_data['picture']):

            raw_image_data = pform.cleaned_data['picture'].read()
            thumbnail_content = resize_image(raw_image_data)
            filename = pform.cleaned_data['picture'].name
            filename = os.path.splitext(filename)[0] + ".jpg"
            new_profile.picture.save(filename, thumbnail_content)

        new_profile.save()


traceback
=======
IOError at /profile/update
cannot identify image file

Traceback:
File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py"
in get_response
  86.                 response = callback(request, *callback_args,
**callback_kwargs)
File "/usr/lib/python2.4/site-packages/django/contrib/auth/
decorators.py" in __call__
  67.             return self.view_func(request, *args, **kwargs)
File "/var/mysite/views.py" in profile_update
  705.             thumbnail_content = resize_image
(raw_image_data)
File "/var/mysite/views.py" in resize_image
  677.     image = PILImage.open(f)
File "/usr/lib/python2.4/site-packages/PIL/Image.py" in open
  1745.     raise IOError("cannot identify image file")

Exception Type: IOError at /profile/update
Exception Value: cannot identify image file


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