Friday, October 24, 2014

Problem with Pillow image resize and save

Hi,
I am trying to put together some code for resizing an image and storing the resized image on the database, but I am running into some problems having struggled to find some decent examples online.  Here is what I have come up with so far, in the model:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    website = models.URLField(blank=True)
    picture = models.ImageField(upload_to='profile_images', blank=True)
    thumb = models.ImageField(upload_to='profile_images', blank=True)

.. and in the view:

def register(request):
    context = RequestContext(request)
    registered = False
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']
                thumb = Image.open(request.FILES['picture'])
                profile.thumb = thumb.resize((200, 200), Image.ANTIALIAS)
            profile.save()
            registered = True
        else:
            print(user_form.errors, profile_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileForm()
    context_dict = {}
    context_dict['user_form'] = user_form
    context_dict['profile_form'] = profile_form
    context_dict['registered'] = registered
    return render_to_response('register.html', context_dict, context)

The error is as follows:
Request Method: POST
Request URL: http://127.0.0.1:8000/register/
Django Version: 1.7
Exception Type: AttributeError
Exception Value:
_committed
Exception Location: C:\landy\lib\site-packages\PIL\Image.py in __getattr__, line 608

Any ideas?
Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1dcaaf85-0841-4ff3-bba2-208d9ee0bce1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment