[Answered ]-How can i take image_width and image_height from resized image, and save them?

1πŸ‘

I’m going to guess that the problem is that image_height and image_width are being populated with the ORIGINAL height and width, before the resize. If it’s different, please update your question.

Without looking at the ImageField code, I’m also going to guess that this will be fixed if you move the super(ResizedImageField, self).save() to the bottom of your new method, after you resize then image, rather than the top.

πŸ‘€Issac Kelly

1πŸ‘

Something like this working for me (it is a little fixed update_dimension_fields method of ImageField):

class MyImageField(ImageField):
    def update_dimension_fields(self, instance, force=False, *args, **kwargs):
        """
        Updates field's width and height fields, if defined.

        This method is hooked up to model's post_init signal to update
        dimensions after instantiating a model instance.  However, dimensions
        won't be updated if the dimensions fields are already populated.  This
        avoids unnecessary recalculation when loading an object from the
        database.

        Dimensions can be forced to update with force=True, which is how
        ImageFileDescriptor.__set__ calls this method.
        """
        # Nothing to update if the field doesn't have have dimension fields.
        has_dimension_fields = self.width_field or self.height_field
        if not has_dimension_fields:
            return

        # getattr will call the ImageFileDescriptor's __get__ method, which
        # coerces the assigned value into an instance of self.attr_class
        # (ImageFieldFile in this case).
        file = getattr(instance, self.attname)

        # Nothing to update if we have no file and not being forced to update.
        if not file and not force:
            return

        dimension_fields_filled = not(
            (self.width_field and not getattr(instance, self.width_field))
            or (self.height_field and not getattr(instance, self.height_field))
        )
        # When both dimension fields have values, we are most likely loading
        # data from the database or updating an image field that already had
        # an image stored.  In the first case, we don't want to update the
        # dimension fields because we are already getting their values from the
        # database.  In the second case, we do want to update the dimensions
        # fields and will skip this return because force will be True since we
        # were called from ImageFileDescriptor.__set__.
        if dimension_fields_filled and not force:
            return

        # file should be an instance of ImageFieldFile or should be None.
        if file:
            width = file.width
            height = file.height
        else:
            # No file, so clear dimensions fields.
            width = None
            height = None

        max_width = 800
        max_height = 1200 
        x1 = width
        y1 = height         

        if x1 > max_width and y1 > max_height:
            x2 = max_width
            y2 = int(float(x2)/float(x1) * y1) - 1
        elif x1 > max_width and y1 <= max_height:
            x2 = max_width
            y2 = int(float(x2)/float(x1) * y1) - 1
        elif x1 <= max_width and y1 > max_height:
            y2 = max_height
            x2 = int(float(y2)/float(y1) * x1) - 1
            if x2 > max_width:
                x2 = max_width
                y2 = int(float(x2)/float(x1) * y1) - 1                
        elif x1 <= max_width and y1 <= max_height:
            y2 = y1
            x2 = x1

        # Update the width and height fields.
        if self.width_field:
            setattr(instance, self.width_field, x2)
        if self.height_field:
            setattr(instance, self.height_field, y2)




    class ResizedImageField(MyImageField):
        attr_class = ResizedImageFieldFile    
        def __init__(self, *args, **kwargs):
            super(ResizedImageField, self).__init__(*args, **kwargs)
πŸ‘€Cadilac

Leave a comment