code: def autorotate(image, orientation=None):
      
      orientation_value = orientation if orientation else \
          image._getexif().get(EXIF_KEYS.get('Orientation'))
      if orientation_value is None:
          raise ImDirectException("No orientation available in Exif "
                                  "tag or given explicitly.")
  
      if orientation_value in (1, 2):
          i = image
      elif orientation_value in (3, 4):
          i = image.transpose(Image.ROTATE_180)
      elif orientation_value in (5, 6):
          i = image.transpose(Image.ROTATE_270)
      elif orientation_value in (7, 8):
          i = image.transpose(Image.ROTATE_90)
      else:
          i = image
  
      if orientation_value in (2, 4, 5, 7):
          i = i.transpose(Image.FLIP_LEFT_RIGHT)
  
      return i
docs: Rotate and return an image according to its Exif information.
  
      ROTATION_NEEDED = {
          1: 0,
          2: 0 (Mirrored),
          3: 180,
          4: 180 (Mirrored),
          5: -90 (Mirrored),
          6: -90,
          7: 90 (Mirrored),
          8: 90,
      }
  
      Args:
          image (PIL.Image.Image): PIL image to rotate
          orientation (): Optional orientation value in [1, 8]
  
      Returns:
          A :py:class:`~PIL.Image.Image` image.