code: def fix_orientation(image):
      
  
      exif_orientation_tag = 0x0112
      exif_transpose_sequences = [
          [],
          [],
          [PIL.Image.FLIP_LEFT_RIGHT],
          [PIL.Image.ROTATE_180],
          [PIL.Image.FLIP_TOP_BOTTOM],
          [PIL.Image.FLIP_LEFT_RIGHT, PIL.Image.ROTATE_90],
          [PIL.Image.ROTATE_270],
          [PIL.Image.FLIP_TOP_BOTTOM, PIL.Image.ROTATE_90],
          [PIL.Image.ROTATE_90],
      ]
  
      try:
          # pylint:disable=protected-access
          orientation = image._getexif()[exif_orientation_tag]
          sequence = exif_transpose_sequences[orientation]
          return functools.reduce(type(image).transpose, sequence, image)
      except (TypeError, AttributeError, KeyError):
          # either no EXIF tags or no orientation tag
          pass
      return image
docs: adapted from https://stackoverflow.com/a/30462851/318857
  
          Apply Image.transpose to ensure 0th row of pixels is at the visual
          top of the image, and 0th column is the visual left-hand side.
          Return the original image if unable to determine the orientation.
  
          As per CIPA DC-008-2012, the orientation field contains an integer,
          1 through 8. Other values are reserved.