Thank you!
I will youse TiNo's solutions. The resize feature is huge!
On Aug 21, 6:10 pm, Alex Gaynor <alex.gay...@gmail.com> wrote:
> On Fri, Aug 21, 2009 at 3:50 AM, TiNo<tin...@gmail.com> wrote:
> > On Fri, Aug 21, 2009 at 10:40, Léon Dignòn <leon.dig...@gmail.com> 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
>
> If you call django.core.files.images. get_image_dimensions on the file
> you'll get back a width, height dimension tuple.
>
> Alex
>
> --
> "I disapprove of what you say, but I will defend to the death your
> right to say it." -- Voltaire
> "The people's good is the highest law." -- Cicero
> "Code can always be simpler than you think, but never as simple as you
> want" -- Me- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---