<s> import zlib <EOL> from hashlib import sha1 <EOL> from django . core . files . base import File <EOL> from django . core . files . storage import default_storage <EOL> from django . core . files . temp import NamedTemporaryFile <EOL> from django . utils . encoding import smart_bytes <EOL> import six <EOL> try : <EOL> from PIL import Image , ImageFile , ExifTags <EOL> except ImportError : <EOL> import Image <EOL> import ImageFile <EOL> import ExifTags <EOL> KEEP_FORMATS = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> DEFAULT_FORMAT = '<STR_LIT>' <EOL> ORIENTATION_TO_TRANSPOSE = { <EOL> <NUM_LIT:1> : None , <EOL> <NUM_LIT:2> : ( Image . FLIP_LEFT_RIGHT , ) , <EOL> <NUM_LIT:3> : ( Image . ROTATE_180 , ) , <EOL> <NUM_LIT:4> : ( Image . ROTATE_180 , Image . FLIP_LEFT_RIGHT , ) , <EOL> <NUM_LIT:5> : ( Image . ROTATE_270 , Image . FLIP_LEFT_RIGHT , ) , <EOL> <NUM_LIT:6> : ( Image . ROTATE_270 , ) , <EOL> <NUM_LIT:7> : ( Image . ROTATE_90 , Image . FLIP_LEFT_RIGHT , ) , <EOL> <NUM_LIT:8> : ( Image . ROTATE_90 , ) , <EOL> } <EOL> ROTATION_TAGS = ( <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ) <EOL> EXIF_TAGS = dict ( ( y , x ) for ( x , y ) in six . iteritems ( ExifTags . TAGS ) ) <EOL> def make_hash ( * args , ** kwargs ) : <EOL> start = kwargs . get ( '<STR_LIT:start>' , None ) <EOL> stop = kwargs . get ( '<STR_LIT>' , None ) <EOL> step = kwargs . get ( '<STR_LIT>' , None ) <EOL> return sha1 ( smart_bytes ( u'<STR_LIT>' . join ( [ <EOL> six . text_type ( arg ) for arg in args ] ) <EOL> ) ) . hexdigest ( ) [ start : stop : step ] <EOL> def get_exif_orientation ( image ) : <EOL> try : <EOL> exif_data = image . _getexif ( ) <EOL> except AttributeError : <EOL> return None <EOL> if exif_data is not None and EXIF_TAGS [ '<STR_LIT>' ] in exif_data : <EOL> orientation = exif_data [ EXIF_TAGS [ '<STR_LIT>' ] ] <EOL> return orientation <EOL> return None <EOL> def apply_exif_orientation ( image ) : <EOL> """<STR_LIT>""" <EOL> orientation = get_exif_orientation ( image ) <EOL> if orientation is not None : <EOL> transpositions = ORIENTATION_TO_TRANSPOSE [ orientation ] <EOL> if transpositions : <EOL> for t in transpositions : <EOL> image = image . transpose ( t ) <EOL> return image <EOL> def exif_aware_size ( image ) : <EOL> """<STR_LIT>""" <EOL> orientation = get_exif_orientation ( image ) <EOL> if orientation in ROTATION_TAGS : <EOL> return image . size [ : : - <NUM_LIT:1> ] <EOL> return image . size <EOL> def exif_aware_resize ( image , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> image = apply_exif_orientation ( image ) <EOL> return image . resize ( * args , ** kwargs ) <EOL> def get_image_dimensions ( file_or_path , close = False ) : <EOL> """<STR_LIT>""" <EOL> p = ImageFile . Parser ( ) <EOL> if hasattr ( file_or_path , '<STR_LIT>' ) : <EOL> file = file_or_path <EOL> file_pos = file . tell ( ) <EOL> file . seek ( <NUM_LIT:0> ) <EOL> else : <EOL> file = open ( file_or_path , '<STR_LIT:rb>' ) <EOL> close = True <EOL> try : <EOL> chunk_size = <NUM_LIT> <EOL> while <NUM_LIT:1> : <EOL> data = file . read ( chunk_size ) <EOL> if not data : <EOL> break <EOL> try : <EOL> p . feed ( data ) <EOL> except zlib . error as e : <EOL> if e . args [ <NUM_LIT:0> ] . startswith ( "<STR_LIT>" ) : <EOL> pass <EOL> else : <EOL> raise <EOL> if p . image : <EOL> return exif_aware_size ( p . image ) <EOL> chunk_size *= <NUM_LIT:2> <EOL> return None <EOL> finally : <EOL> if close : <EOL> file . close ( ) <EOL> else : <EOL> file . seek ( file_pos ) <EOL> def save_image ( <EOL> image , <EOL> storage_path , <EOL> format = DEFAULT_FORMAT , <EOL> storage = default_storage ) : <EOL> """<STR_LIT>""" <EOL> if format not in KEEP_FORMATS : <EOL> format = DEFAULT_FORMAT <EOL> with NamedTemporaryFile ( ) as temp : <EOL> image . save ( temp , format = format ) <EOL> return storage . save ( storage_path , File ( temp ) ) </s>
