1

I want to write a method which detects the orientation of uploaded image, then use a thumb with certain landscape or portrait dimensions using django-imagekit. Any ideas?

class Photo(models.Model):

title = models.CharField(max_length=200)
photo = models.ImageField(upload_to='/photos', max_length=50)
photoalbum = models.ForeignKey(Photoalbum, related_name='photos')
portrait_thumb = ImageSpecField(source='photo',
                           processors=[ResizeToFill(50, 100)],
                           format='JPEG',
                           options={'quality': 60})
landscape_thumb = ImageSpecField(source='photo',
                           processors=[ResizeToFill(100, 50)],
                           format='JPEG',
                           options={'quality': 60})

def get_orientation(self):
   '''HOW TO IMPLEMENT THIS?'''

def get_thumb_url(self):
    if self.get_orientation == 'portrait':
        return self.portrait_thumb.url()
    elif self.get_orientation == 'landscape':
        return self.landscape_thumb.url()
1
  • download picture, check its width and length and do a comparison?
    – Sara S
    Apr 27, 2014 at 19:31

1 Answer 1

3

You can do this by checking the width and height attributes of the model's image spec field. If width is greater than height, it is landscape. If the height is bigger than the width it is portrait.

2
  • Pretty simple :) Thanks. I didn't know about width and height attributes.
    – maremare
    Apr 27, 2014 at 19:55
  • I guess that's not exactly what @maremare asked, but there's a chance the image might be upside down with this approach
    – felix
    May 24, 2017 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.