Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How can i take image_width and image_height from resized image, and save them?
- 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)
- <table>
- <tbody>
- <tr>
- <td style="max-width: 800px; overflow-x: auto; position: relative">
- <img style="position: relative;" width="{{ original.image_width }}" height="{{ original.image_height }}" src="{{ original.image.url }}" id="cropbox" alt="" />
- </td>
- <td>
- <p style="text-align: left; margin-left: 10px;">Your avatar</p>
- <div style="width: 180px; height: 180px; margin-left: 10px; margin-bottom: 10px; overflow: hidden;">
- <img src="{{ original.image.url }}" id="preview" alt="" />
- </div>
- </td>
- </tr>
- </tbody>
- </table>
- 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)
Add Comment
Please, Sign In to add comment