Created
May 6, 2025 23:54
-
-
Save secemp9/47a2634a03b4f76f6798e59c6781e309 to your computer and use it in GitHub Desktop.
autocolor (photoshop) inspired algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def improved_auto_color(image: Image.Image) -> Image.Image: | |
""" | |
Improved version of Photoshop-like Auto Color. | |
It works by: | |
1. Converting to float and normalizing. | |
2. Computing per-channel percentiles (e.g., 0.5% and 99.5%). | |
3. Stretching values between those percentiles to [0, 255]. | |
4. This avoids over-amplifying noise or outliers. | |
""" | |
img_array = np.array(image).astype(np.float32) | |
# Process each channel independently | |
for channel in range(3): | |
channel_data = img_array[..., channel] | |
low_percentile = np.percentile(channel_data, 0.5) | |
high_percentile = np.percentile(channel_data, 99.5) | |
if high_percentile > low_percentile: | |
# Stretch values between low and high percentiles to [0, 255] | |
stretched = (channel_data - low_percentile) * 255.0 / (high_percentile - low_percentile) | |
img_array[..., channel] = np.clip(stretched, 0, 255) | |
return Image.fromarray(img_array.astype(np.uint8)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment