Skip to content

Instantly share code, notes, and snippets.

@ialhashim
Last active January 3, 2023 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ialhashim/1a1b2fcab3874c126f23cae56bc2e0cf to your computer and use it in GitHub Desktop.
Save ialhashim/1a1b2fcab3874c126f23cae56bc2e0cf to your computer and use it in GitHub Desktop.
Apply color vibrance adjustments
# Copy from https://github.com/bbonik/image_enhancement/blob/f0c286a166443202d592b920b63ee6901a0ee727/source/image_enhancement.py#L1388
import numpy as np
from skimage import img_as_float
def change_color_saturation(
image_color,
image_ph_mask=None,
sat_degree=1.5,):
LOCAL_BOOST = 0.2
THRESHOLD_DARK_TONES = 100 / 255
#TODO: return the same image type
image_color = img_as_float(image_color) # [0,1]
# define gray scale
image_gray = (image_color[:,:,0] +
image_color[:,:,1] +
image_color[:,:,2]) / 3
image_gray = np.dstack([image_gray] * 3) # grayscale with 3 channels
image_delta = image_color - image_gray # deviations from gray
# defining local color amplification degree
if image_ph_mask is not None:
detail_amplification_local = image_ph_mask / THRESHOLD_DARK_TONES
detail_amplification_local[detail_amplification_local>1] = 1
detail_amplification_local = ((1 - detail_amplification_local) *
LOCAL_BOOST) + 1 # [1, 1.2]
detail_amplification_local = np.dstack(
[detail_amplification_local] * 3) # 3 channels
else:
detail_amplification_local = 1
image_new_sat = (image_gray +
image_delta * sat_degree * detail_amplification_local)
image_new_sat = np.clip(
a=image_new_sat,
a_min=0,
a_max=1,
out=image_new_sat
)
return image_new_sat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment