How to resize before uploading pictures in Django


1 comment

  • Avatar
    Loic Verger Del Bove

    Hi Yiwei,

    Thanks for reaching out.

    If you want to add transformations during the upload, you will need to use the CloudinaryFileField class as mentioned here:

    This is our

    from django.db import models
    from cloudinary.models import CloudinaryField
    class Photo(models.Model):
      image = CloudinaryField('image')

    And this is our

    from django.forms import ModelForm      
    from cloudinary.forms import CloudinaryFileField      
    from .models import Photo
    class PhotoForm(ModelForm):
      class Meta:
        model = Photo
      image = CloudinaryFileField(
        attrs = { 'style': "margin-top: 30px" }, 
        options = { 
          'tags': "directly_uploaded",
          'crop': 'limit', 'width': 1000, 'height': 1000,
          'eager': [{ 'crop': 'fill', 'width': 150, 'height': 100 }]

    While the eager will create a 150x100px derived image, the transformation 'crop': 'limit', 'width': 1000, 'height': 1000 will resize the image to a 1000x1000px when saving it.

    Hope it helps.



    Comment actions Permalink

Please sign in to leave a comment.