Posts

Django Models images + Crop

First create an Image model: class Image(models.Model): image = models.ImageField(upload_to="images") class Meta: verbose_name = _('Image') verbose_name_plural = _('Images') Then we add a Crop model to carry crop related data for each image: class Crop(models.Model): image = models.OneToOneField(Image, related_name='crop', on_delete=models.CASCADE) x = models.IntegerField() y = models.IntegerField() width = models.IntegerField() height = models.IntegerField() class Meta: verbose_name = _('Crop') verbose_name_plural = _('Crops') The x, y and width and height fields will define the image metadata for our crop. Like in HTML, these are canvas coordinates where the upper left starts at coordinates (0, 0) x and y indicates our starting point of the crop relative to the image from the top left x , y axis. The width indicates distance we go from the x ...