If you need it only for one particular form, you can also override
clean_<fieldname>():
---------------------------
import Image # PIL

class UserProfileForm(ModelForm):
    class Meta:
        model = UserProfile

    # Model UserProfile has an ImageField named avatar.
    def clean_avatar(self):
        img = self.cleaned_data['avatar'] # img will be a PIL object
        image = Image.open(img)
        if image.size[0] > 64: #image.size is a 2-tuple (width,
height)
            raise forms.ValidationError("Not more than 64*64 pixels.")
        if img.size > 10000:
            raise forms.ValidationError("Filesize too big. Max. 10k")
        #if img.width > 64:

        return img
---------------------------

You could do all your stuff with the Python Imaging Library in the
clean_... function. I got this hint from another thread:
http://groups.google.com/group/django-users/browse_thread/thread/8ba69b6846e84dd3

I do not know much about the PIL, so maybe it has not the funtionality
you are looking for. Also whether this is worse or better than
creating a custom ImageField, I don't know.

-ld

On Aug 22, 12:03 am, Jon <scenesh...@googlemail.com> wrote:
> I want to expand on the ImageField functionality of django. Mainly, I
> want to check the dimensions of the image the user is uploading, and
> resize / crop where appropriate. I would also like to set compression
> amount, and perhaps even allow for watermarking. I know there are
> several libraries out there that do this, but I want to keep it as
> lightweight as possible, and haven't had much luck with sorl.
>
> It seems to me the best way to do this would be to create a custom
> field type, so I could do something like this in my model:
>
> image_tb = models.CustomImageField("Thumbnail Image", upload_to
> ='uploads/projects', size=(50, 50), compression="60", watermark="path/
> to/watermark.png")
>
> Is this the best way to do something like this? I understand that you
> could also hijack the save method, but I might want to have more than
> one image field in each model meaning the sizes might vary. Can anyone
> point me towards any articles which will help me on my way?
--~--~---------~--~----~------------~-------~--~----~
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