Guest User

Untitled

a guest
Aug 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. How can i take image_width and image_height from resized image, and save them?
  2. class Original(models.Model):
  3. image = ResizedImageField(
  4. blank=True, null=True,
  5. verbose_name = _('Original image'),
  6. upload_to = upload_image,
  7. width_field = 'image_width',
  8. height_field = 'image_height',
  9. validators = [dimension_validator],
  10. )
  11. image_width = models.PositiveIntegerField(
  12. blank=True, null=True,
  13. verbose_name = _('Image width'),
  14. editable = False,
  15. default = 0,
  16. )
  17. image_height = models.PositiveIntegerField(
  18. blank=True, null=True,
  19. verbose_name = _('Image height'),
  20. editable = False,
  21. default = 0,
  22. )
  23.  
  24.  
  25.  
  26.  
  27. class ResizedImageFieldFile(ImageFieldFile):
  28.  
  29. def save(self, name, content, save=True):
  30. super(ResizedImageFieldFile, self).save(name, content, save)
  31. img = Image.open(self.path)
  32. x1 = img.size[0]
  33. y1 = img.size[-1]
  34. max_width = 800
  35. max_height = 1200
  36.  
  37. if x1 > max_width and y1 > max_height:
  38. x2 = max_width
  39. y2 = int(float(x2)/float(x1) * y1) - 1
  40. elif x1 > max_width and y1 <= max_height:
  41. x2 = max_width
  42. y2 = int(float(x2)/float(x1) * y1) - 1
  43. elif x1 <= max_width and y1 > max_height:
  44. y2 = max_height
  45. x2 = int(float(y2)/float(y1) * x1) - 1
  46. if x2 > max_width:
  47. x2 = max_width
  48. y2 = int(float(x2)/float(x1) * y1) - 1
  49. elif x1 <= max_width and y1 <= max_height:
  50. y2 = y1
  51. x2 = x1
  52. if y2 != y1 and x2 != x1:
  53. img = img.resize((x2, y2), Image.ANTIALIAS)
  54. img.save(self.path)
  55.  
  56. def delete(self, save=True):
  57. os.remove(self.path)
  58. super(ResizedImageFieldFile, self).delete(save)
  59.  
  60.  
  61. class ResizedImageField(ImageField):
  62. attr_class = ResizedImageFieldFile
  63. def __init__(self, *args, **kwargs):
  64. super(ResizedImageField, self).__init__(*args, **kwargs)
  65.  
  66. <table>
  67. <tbody>
  68. <tr>
  69. <td style="max-width: 800px; overflow-x: auto; position: relative">
  70. <img style="position: relative;" width="{{ original.image_width }}" height="{{ original.image_height }}" src="{{ original.image.url }}" id="cropbox" alt="" />
  71. </td>
  72. <td>
  73. <p style="text-align: left; margin-left: 10px;">Your avatar</p>
  74. <div style="width: 180px; height: 180px; margin-left: 10px; margin-bottom: 10px; overflow: hidden;">
  75. <img src="{{ original.image.url }}" id="preview" alt="" />
  76. </div>
  77. </td>
  78. </tr>
  79. </tbody>
  80. </table>
  81.  
  82. class MyImageField(ImageField):
  83. def update_dimension_fields(self, instance, force=False, *args, **kwargs):
  84. """
  85. Updates field's width and height fields, if defined.
  86.  
  87. This method is hooked up to model's post_init signal to update
  88. dimensions after instantiating a model instance. However, dimensions
  89. won't be updated if the dimensions fields are already populated. This
  90. avoids unnecessary recalculation when loading an object from the
  91. database.
  92.  
  93. Dimensions can be forced to update with force=True, which is how
  94. ImageFileDescriptor.__set__ calls this method.
  95. """
  96. # Nothing to update if the field doesn't have have dimension fields.
  97. has_dimension_fields = self.width_field or self.height_field
  98. if not has_dimension_fields:
  99. return
  100.  
  101. # getattr will call the ImageFileDescriptor's __get__ method, which
  102. # coerces the assigned value into an instance of self.attr_class
  103. # (ImageFieldFile in this case).
  104. file = getattr(instance, self.attname)
  105.  
  106. # Nothing to update if we have no file and not being forced to update.
  107. if not file and not force:
  108. return
  109.  
  110. dimension_fields_filled = not(
  111. (self.width_field and not getattr(instance, self.width_field))
  112. or (self.height_field and not getattr(instance, self.height_field))
  113. )
  114. # When both dimension fields have values, we are most likely loading
  115. # data from the database or updating an image field that already had
  116. # an image stored. In the first case, we don't want to update the
  117. # dimension fields because we are already getting their values from the
  118. # database. In the second case, we do want to update the dimensions
  119. # fields and will skip this return because force will be True since we
  120. # were called from ImageFileDescriptor.__set__.
  121. if dimension_fields_filled and not force:
  122. return
  123.  
  124. # file should be an instance of ImageFieldFile or should be None.
  125. if file:
  126. width = file.width
  127. height = file.height
  128. else:
  129. # No file, so clear dimensions fields.
  130. width = None
  131. height = None
  132.  
  133. max_width = 800
  134. max_height = 1200
  135. x1 = width
  136. y1 = height
  137.  
  138. if x1 > max_width and y1 > max_height:
  139. x2 = max_width
  140. y2 = int(float(x2)/float(x1) * y1) - 1
  141. elif x1 > max_width and y1 <= max_height:
  142. x2 = max_width
  143. y2 = int(float(x2)/float(x1) * y1) - 1
  144. elif x1 <= max_width and y1 > max_height:
  145. y2 = max_height
  146. x2 = int(float(y2)/float(y1) * x1) - 1
  147. if x2 > max_width:
  148. x2 = max_width
  149. y2 = int(float(x2)/float(x1) * y1) - 1
  150. elif x1 <= max_width and y1 <= max_height:
  151. y2 = y1
  152. x2 = x1
  153.  
  154. # Update the width and height fields.
  155. if self.width_field:
  156. setattr(instance, self.width_field, x2)
  157. if self.height_field:
  158. setattr(instance, self.height_field, y2)
  159.  
  160.  
  161.  
  162.  
  163. class ResizedImageField(MyImageField):
  164. attr_class = ResizedImageFieldFile
  165. def __init__(self, *args, **kwargs):
  166. super(ResizedImageField, self).__init__(*args, **kwargs)
Add Comment
Please, Sign In to add comment