On Fri, Aug 21, 2009 at 10:40, Léon Dignòn <[email protected]> wrote:
>
> Hello,
>
> in my ModelForm I try to allow only images (for avatar) smaller than
> 64*64 pixels. I've done this by overriding clean_avatar(). But how can
> I check the dimensions? The instance is an InMemoryUploadedFile which
> has no width or height. Any help regarding this?
>
> class UserProfileForm(ModelForm):
> class Meta:
> model = UserProfile
> exclude = ('user',) # User will be filled in by the view.
>
> def clean_avatar(self):
> img = self.cleaned_data['avatar']
>
> if img.size > 10000: # working fine!
> raise forms.ValidationError("Filesize too big. Max. 10k")
> if img.width > 64: # 'InMemoryUploadedFile' object has no
> attribute 'width'
> raise forms.ValidationError("Not more than 64*64 pixels.")
> return img
You can do this with PIL:
import Image
image = Image.open(img)
if image.size[0] > 64: #image.size is a 2-tuple (width, height)
...
Or you could resize it down to 64 px if it is bigger.
TiNo
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---