class Musician(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True) name = models.CharField(max_length=255, blank=True, null=True) bio = models.TextField() city = models.CharField(max_length=255) latitude = models.FloatField(blank=True, null=True) longitude = models.FloatField(blank=True, null=True) # fields having to do with money cashapp_name = models.CharField(max_length=255, blank=True, null=True) paypal_donation_url = models.CharField(max_length=255, blank=True, null=True) cashapp_qr = models.ImageField(upload_to="images/", null=True, blank=True) paypal_qr = models.ImageField(upload_to="images/", null=True, blank=True) venmo_qr = models.ImageField(upload_to="images/", null=True, blank=True) favorited_by = models.ManyToManyField(User, related_name="favorite_musician", blank=True) # fields having to do with images headshot = models.ImageField(upload_to="images/", null=True, blank=False) thumbnail = ImageSpecField( source="headshot", processors=[Transpose(), ResizeToCover(200, 200), SmartCrop(200, 200)], format="JPEG", options={"quality": 100}, ) full_cover = ImageSpecField( source="headshot", processors=[Transpose(), ResizeToFit(600, 600), SmartCrop(400, 400)], format="JPEG", options={"quality": 100}, ) very_small_thumb = ImageSpecField( source="headshot", processors=[Transpose(), ResizeToCover(100, 100), SmartCrop(100, 100)], format="JPEG", options={"quality": 100}, ) def __str__(self): return f'{self.name}'
def processors(self): model, field_name = get_field_info(self.source) min_dim = min(model.original.width, model.original.height) return [ SmartCrop(width=min_dim, height=min_dim), ResizeToFill(width=500, height=500) ]
class TestForm(forms.ModelForm): image = ikforms.ProcessedImageField(spec_id='tests:testform_image', processors=[SmartCrop(50, 50)], format='JPEG') class Meta: model = ImageModel
class Photo(models.Model): original_image = models.ImageField(upload_to='photos') # Implicit source field thumbnail = ImageSpecField( [Adjust(contrast=1.2, sharpness=1.1), ResizeToFill(50, 50)], format='JPEG', options={'quality': 90}) smartcropped_thumbnail = ImageSpecField( [Adjust(contrast=1.2, sharpness=1.1), SmartCrop(50, 50)], source='original_image', format='JPEG', options={'quality': 90})
class ProcessedImageFieldModel(models.Model): processed = ProcessedImageField([SmartCrop(50, 50)], format='JPEG', options={'quality': 90}, upload_to='p')
def processors(self): model, field_name = get_field_info(self.source) crop_width = int(model.original.width) crop_height = min(model.original.height, int(crop_width * 3.0 / 5.0)) return [SmartCrop(width=crop_width, height=crop_height)]