jdriscoll / django-imagekit

Automates image processing for Django models. Resize, process and cache multiple versions of your image files. Access newly created files with a standard API. Supports alternate storage schemes such as Amazon S3.

Clone this repository (size: 135.6 KB): HTTPS / SSH
$ hg clone http://hg.driscolldev.com/django-imagekit
follow

#14 Exif auto rotate

Reported by Anonymous, created 14 months ago, last edited unknown.

A suggestion for a processor. I haven't extensively tested it but it should come in extremely handy and arguably should be default for all displays/thumbnails:

from PIL import Image
class ExifRotate(processors.ImageProcessor):
    """Autorotates an image based on exif orientation"""
    method={1:[],
            2:[Image.FLIP_LEFT_RIGHT],
            3:[Image.ROTATE_180],
            4:[Image.FLIP_TOP_BOTTOM],
            5:[Image.ROTATE_270, Image.FLIP_LEFT_RIGHT],
            6:[Image.ROTATE_270],
            7:[Image.ROTATE_90, Image.FLIP_LEFT_RIGHT],
            8:[Image.ROTATE_90],
            }
    @classmethod
    def process(cls, img, fmt, obj):
        orient = int(obj.EXIF['Image Orientation'].values[0])
        for step in cls.method[orient]:
            img=img.transpose(step)
        return img, fmt

Or, if there's an easier way to do this, I'd be glad to know!

Status: resolved Responsible: nobody Type: enhancement
Milestone: none Component: none Version: none

Attachments

No attachments added for this issue yet.

Comments and changes

#1 Anonymous

written 14 months ago, last edited unknown.

Forgot to mention, I copied over the EXIF method from Photologue into my ImageKit Photo model


#2

Justin Driscoll / jdriscoll

→ Changed type from bug to enhancement.

→ Changed status from new to resolved.

I've added an "auto" method to the Transpose processor based on your code. It doesn't require the EXIF.py modue however, or modifying the ImageModel. I'd be grateful if you can help me test it out.


#3

bnewbold

I could be wrong, but I don't think that the auto rotate solution works for non-file storages; it doesn't for me when I add a transpose processor (i'm using S3Storage for original images and file system for processed images).

The Image creation on line 169 of processors.py just tries to .open the file object; it seems like for remote storage systems the file needs to be read into a StringIO first, a la line 62 of specs.py. I could submit a patch but i'm sleepy; can the orientation get pulled out of 'img' instead of reloading the whole file into a string buffer?


#4 Anonymous

written 7 months ago, last edited unknown.

@Justin "I've added an "auto" method to the Transpose processor based on your code. It doesn't require the EXIF.py modue however, or modifying the ImageModel. I'd be grateful if you can help me test it out."

Could you post a code example for the use of this method? I'm hoping to use it to autorotate all thumbnails specified in my ik_specs.py file, but haven't been able to figure out how to access this method so far.


#5

Justin Driscoll / jdriscoll

The "auto" method isn't a method of the class it's an option (the default option actually) for the method property of the Transpose processor. To use it you would just add it to the processors list in your specificiation:

1
2
3
4
5
6
from imagekit.specs import ImageSpec
from imagekit import processors

class MyImageSpec(ImageSpec):
    access_as = 'my_image'
    processors = [processors.Transpose]

#6 Anonymous

written 7 months ago, last edited unknown.

I see, thanks. I had already tried adding transpose as a processor in the way you mentioned, so perhaps something else is going wrong for me. Here's how a couple of classes from my specification look:

from imagekit.specs import ImageSpec 
from imagekit import processors 

class ResizeDisplay(processors.Resize):
    width = 900

class ResizeNormal(processors.Resize): 
    width = 450

class Display(ImageSpec):
        quality = 90
        processors = [processors.Transpose, ResizeDisplay]

class Normal(ImageSpec):
        quality = 90
        access_as = 'normal' 
        pre_cache = True
        processors = [processors.Transpose, ResizeNormal]

When I use imagespecs set up like this, if i upload portrait format JPG photos taken with a nikon d90, the thumbnails generated aren't being rotated correctly. (I took the liberty of mailing you a link to one of these images in case you can spot whether this is a bug, or a weird image format, or a user error).


Add comment / attachment

Show/hide preview

Verification: Please write the text from the image in the box (letters only)

captcha

Is that you, Humanoid? Is this me?