Opens, identifies the given image file, and rotates it if it is a JPEG.

    Note that this method does NOT employ the lazy loading methodology that
    the PIL Images otherwise use. This is done to avoid having to save new

    Args:
        fp: A filename (string), pathlib.Path object or a file-like object.

    Returns:
        The image as an :py:class:`~PIL.Image.Image` object.

    Raises:
        IOError: If the file cannot be found, or the image cannot be
            opened and identified.

def imdirect_open(fp):
    """Opens, identifies the given image file, and rotates it if it is a JPEG.

    Note that this method does NOT employ the lazy loading methodology that
    the PIL Images otherwise use. This is done to avoid having to save new

    Args:
        fp: A filename (string), pathlib.Path object or a file-like object.

    Returns:
        The image as an :py:class:`~PIL.Image.Image` object.

    Raises:
        IOError: If the file cannot be found, or the image cannot be
            opened and identified.

    """

    img = pil_open(fp, 'r')
    if img.format == 'JPEG':
        # Read Exif tag on image.
        if isinstance(fp, string_types):
            exif = piexif.load(text_type_to_use(fp))
        else:
            fp.seek(0)
            exif = piexif.load(fp.read())

        # If orientation field is missing or equal to 1, nothing needs to be done.
        orientation_value = exif.get('0th', {}).get(piexif.ImageIFD.Orientation)
        if orientation_value is None or orientation_value == 1:
            return img
        # Otherwise, rotate the image and update the exif accordingly.
        img_rot = autorotate(img)
        exif = update_exif_for_rotated_image(exif)

        # Now, lets restore the output image to
        # PIL.JpegImagePlugin.JpegImageFile class with the correct,
        # updated Exif information.
        # Save image as JPEG to get a correct byte representation of
        # the image and then read it back.
        with io.BytesIO() as bio:
            img_rot.save(bio, format='jpeg', exif=piexif.dump(exif))
            bio.seek(0)
            img_rot_new = pil_open(bio, 'r')
            # Since we use a BytesIO we need to avoid the lazy
            # loading of the PIL image. Therefore, we explicitly
            # load the data here.
            img_rot_new.load()
        img = img_rot_new

    return img