# External library
import cv2

# Standard library
import os


def clahe_image(image_path: str, output_path: str) -> str:
    """Improves contrast in images.

    Args:
        image_path (str): Input path.
        output_path (str): Path of result image.

    Returns:
        output_path (str): Path of result image.

    """
    try:
        if not os.path.isfile(image_path):
            raise FileNotFoundError("File path not found.")
        
        image = cv2.imread(image_path)
        ycrcb_img = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)

        # Applying CLAHE to L-channel
        # feel free to try different values for the limit and grid size:
        clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
        ycrcb_img[:, :, 0] = clahe.apply(ycrcb_img[:, :, 0])
        output = cv2.cvtColor(ycrcb_img, cv2.COLOR_YCrCb2BGR)

        output_dir = os.path.dirname(output_path)

        os.makedirs(output_dir, exist_ok= True)
        cv2.imwrite(output_path, output)

    except Exception as ex:
        print("errors:", ex)
        output_path = None

    return output_path
