Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping auto-rotation of images in Django-imagekit Thumbnail

I've got a thumbnail filter that always ends up rotating the image 90 degrees to the left when the image is taller than it is wide (I've checked, and the original image is straight, while the cached image is rotated). The relevant code looks like this:

profile_image = models.ImageField(upload_to='profile_images', default='profile_images/icon.png')
profile_icon = ImageSpecField(source='profile_image',
                              processors=[processors.Thumbnail(width=72, height=72, crop=True)],
                              format='JPEG',
                              options={'quality': 60})

How do I stop the auto-rotation?

like image 539
timesuji Avatar asked Jun 29 '13 23:06

timesuji


3 Answers

Glad you figured this out, but ImageKit may be able to help you out some still. Check out the Transpose processor (imagekit.processors.Transpose). By default, it will use the metadata in the image, and rotate by that amount! Just be sure to list this processor first as subsequent processors will strip the metadata from the image.

like image 101
matthewwithanm Avatar answered Oct 05 '22 02:10

matthewwithanm


To elaborate on matthewwithanm's helpful pointer, the OP's code would be tweaked to look like this:

profile_image = models.ImageField(upload_to='profile_images', default='profile_images/icon.png')
profile_icon = ImageSpecField(source='profile_image',
                              processors=[
                                  processors.Transpose(),
                                  processors.Thumbnail(width=72, height=72, crop=True)
                              ],
                              format='JPEG',
                              options={'quality': 60})

ie, add a call to processors.Transpose() with no arguments.

I had this problem with an original portrait-format image downloaded from Flickr. That image (taken on an iPhone) is in portrait format, and by default imagekit rotates it, in this particular case, 90 degrees anti-clockwise.

like image 32
Phil Gyford Avatar answered Oct 05 '22 02:10

Phil Gyford


Okay, it turns out that it's a problem with the images being uploaded, not anything with Django. Pictures that are taken on an iPhone can have phone orientation metadata that causes the browser to think the photo's natural orientation is sideways. But, if I open that photo in Preview, rotate it left and then back to normal, and then save it again, there are no problems.

Feed image shows rotated in certain browsers

Surprise!

like image 44
timesuji Avatar answered Oct 05 '22 02:10

timesuji