code: def exif_orientation(im):
      
      try:
          exif = im._getexif()
      except Exception:
          # There are many ways that _getexif fails, we're just going to blanket
          # cover them all.
          exif = None
      if exif:
          orientation = exif.get(0x0112)
          if orientation == 2:
              im = im.transpose(Image.FLIP_LEFT_RIGHT)
          elif orientation == 3:
              im = im.transpose(Image.ROTATE_180)
          elif orientation == 4:
              im = im.transpose(Image.FLIP_TOP_BOTTOM)
          elif orientation == 5:
              im = im.transpose(Image.ROTATE_270).transpose(Image.FLIP_LEFT_RIGHT)
          elif orientation == 6:
              im = im.transpose(Image.ROTATE_270)
          elif orientation == 7:
              im = im.transpose(Image.ROTATE_90).transpose(Image.FLIP_LEFT_RIGHT)
          elif orientation == 8:
              im = im.transpose(Image.ROTATE_90)
      return im
docs: Rotate and/or flip an image to respect the image's EXIF orientation data.