如何从调整大小的图像中获取image_width和image_height并保存它们?

时间:2011-10-22 20:58:21

标签: django django-models

如果我希望从image_width获取image_height Original的{​​{1}}个实例,我该怎么办?

ResizedImageField

当用户选择要裁剪的区域时,我需要它们给出适当的图像宽度和高度(不能因为IE而使用溢出属性):

class Original(models.Model):
    image = ResizedImageField(
        blank=True, null=True,
        verbose_name = _('Original image'),
        upload_to    = upload_image,
        width_field  = 'image_width',
        height_field = 'image_height',
        validators   = [dimension_validator],
    )
    image_width = models.PositiveIntegerField(
        blank=True, null=True,
        verbose_name = _('Image width'),
        editable = False,
        default = 0,
    )
    image_height = models.PositiveIntegerField(
        blank=True, null=True,
        verbose_name = _('Image height'),
        editable = False,
        default = 0,
    )




class ResizedImageFieldFile(ImageFieldFile):

    def save(self, name, content, save=True):
        super(ResizedImageFieldFile, self).save(name, content, save)
        img = Image.open(self.path)
        x1 = img.size[0]
        y1 = img.size[-1]
        max_width = 800
        max_height = 1200

        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
        if y2 != y1 and x2 != x1:
            img = img.resize((x2, y2), Image.ANTIALIAS)
            img.save(self.path)

    def delete(self, save=True):
        os.remove(self.path)
        super(ResizedImageFieldFile, self).delete(save)                    


class ResizedImageField(ImageField):
    attr_class = ResizedImageFieldFile    
    def __init__(self, *args, **kwargs):
        super(ResizedImageField, self).__init__(*args, **kwargs)

2 个答案:

答案 0 :(得分:1)

我要猜测问题是在调整大小之前,image_height和image_width正在填充ORIGINAL高度和宽度。如果不同,请更新您的问题。

在没有查看ImageField代码的情况下,我还要猜测,如果你将super(ResizedImageField,self).save()移动到新方法的底部,然后调整图像大小,这将被修复,而不是顶部。

答案 1 :(得分:1)

这样的东西对我有用(它是ImageField的一个小的固定update_dimension_fields方法):

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)