ios - Django rotates iphone image after upload -


i'm working on photo website want user able upload portrait or landscape oriented photo. maximum width should 1250px, maximum height 1667px if it's in portrait mode. when upload photos in portrait orientation, show rotated 90 degrees left. there way using pillow make sure photo stays in correct orientation?

this code:

class result(models.model):     result01        = models.filefield(upload_to=get_upload_file_name, null=true, blank=true)     result01thumb   = models.filefield(upload_to=get_upload_file_name, null=true, blank=true)      def save(self):         super(result, self).save()         if self.result01:             size = 1667, 1250             image = image.open(self.result01)             image.thumbnail(size, image.antialias)             fh = storage.open(self.result01.name, "w")             format = 'png'             image.save(fh, format)             fh.close() 

it's important users able upload photos phones while they're mobile, correct orientation important. there can here?

you can try resize , auto-rotate (based on exif information) image using pillow.

def image_resize_and_autorotate(infile, outfile):     image.open(infile) image:         file_format = image.format         exif = image._getexif()          image.thumbnail((1667, 1250), resample=image.antialias)          # if image has exif data orientation, let's rotate         orientation_key = 274 # cf exiftags         if exif , orientation_key in exif:             orientation = exif[orientation_key]              rotate_values = {                 3: image.rotate_180,                 6: image.rotate_270,                 8: image.rotate_90             }              if orientation in rotate_values:                 image = image.transpose(rotate_values[orientation])          image.save(outfile, file_format) 

Comments