for the cases that no modal value is 2 standart variantions above the mean stretch the contrast instead of clipping. Code: def adjust_grayscale_values(img: np.ndarray) -> tuple[np.ndarray, int, int]:
"""Adjust the values of a grayscale image."""
try:
# Calculate modal white value in the range [250, 255]
hist_white, bins_white = np.histogram(img[img >= 250], bins=6, range=(250, 256))
modal_white = int(bins_white[np.argmax(hist_white)])

# Calculate histogram for the range [0, 55]
hist_0_55, _ = np.histogram(img[(img >= 0) & (img < 56)], bins=56, range=(0, 56))

# Calculate mean and standard deviation for the range [0, 150]
hist_0_150, _ = np.histogram(img[(img >= 0) & (img < 151)], bins=151, range=(0, 151))
mean_0_150 = np.mean(hist_0_150)
std_dev_0_150 = np.std(hist_0_150)

# Calculate modal value for range [0, 40]
hist_0_40 = hist_0_55[:40]
modal_black_narrow = np.argmax(hist_0_40)
std_dev_above_mean_0_40 = (hist_0_40[modal_black_narrow] - mean_0_150) / std_dev_0_150 if std_dev_0_150 != 0 else 0

# Calculate modal value for the range [0, 55]
modal_black_wide = np.argmax(hist_0_55)
std_dev_above_mean_0_55 = (hist_0_55[modal_black_wide] - mean_0_150) / std_dev_0_150 if std_dev_0_150 != 0 else 0

# Set modal_black based on conditions
modal_black = 0

# Use narrow modal if it's 2 standard deviations above the mean
if std_dev_above_mean_0_40 >= 2:
    modal_black = modal_black_narrow
else:
    modal_black = modal_black_wide

# Convert image to float for processing
adjusted = img.astype(np.float64)

# Create a mask for extreme original values (0 and 255)
mask = (img == 0) | (img == 255)

# Exclude extreme values from the white adjustment
white_adjust_mask = ~mask

# Scale image if modal white is not at maximum intensity
if modal_white != 255:
    adjusted[white_adjust_mask] *= 255.0 / modal_white
    adjusted = np.clip(adjusted, 0, 255)

# After white value adjustment, identify new extreme values
new_extremes = (adjusted == 0) | (adjusted == 255)

# Combine masks to exclude all extreme values from black adjustment
total_extremes = mask | new_extremes
black_adjust_mask = ~total_extremes

# Mask for values between 0 and 55
range_0_55_mask = (adjusted >= 0) & (adjusted <= 55)

# Adjust image based on modal black value
if std_dev_above_mean_0_40 < 2 and std_dev_above_mean_0_55 < 2:
    # Clip only values within 0 to 55 to 0 if they are below modal value
    adjusted[range_0_55_mask & black_adjust_mask] = np.clip(
        adjusted[range_0_55_mask & black_adjust_mask], modal_black + 1, None
    ) - (modal_black + 1)
else:
    # Perform traditional adjustment if any is needed
    if modal_black:
        adjusted[black_adjust_mask] = (
            adjusted[black_adjust_mask] - modal_black
        ) * (255.0 / (255 - modal_black))
        adjusted = np.clip(adjusted, 0, 255)

        gamma = 1.0 - (modal_black / 255.0)
        adjusted[black_adjust_mask] = (
            np.power(adjusted[black_adjust_mask] / 255.0, gamma) * 255
        )

# Round to nearest integer and convert to uint8
adjusted = np.rint(adjusted).astype(np.uint8)

return adjusted, modal_black, modal_white

except ValueError as e:
log_error_message(f"Error adjusting values for image: {e}")
return img, 0, 255