且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据RGB值更改图像的颜色

更新时间:2023-01-26 17:59:04

参考链接问题中的解决方案:你可以调整 shift_hue()函数调整色调,饱和度和值而不仅仅是色调。然后,您应该可以根据需要调整所有这些参数。

Refering to the solution in the linked question: You can adjust the shift_hue() function to adjust hue, saturation and value instead of just hue. That should then allow you to shift all of these parameters just as you like.

原文:

def shift_hue(arr, hout):
    r, g, b, a = np.rollaxis(arr, axis=-1)
    h, s, v = rgb_to_hsv(r, g, b)
    h = hout
    r, g, b = hsv_to_rgb(h, s, v)
    arr = np.dstack((r, g, b, a))
    return arr

调整版本:

def shift_hsv(arr, delta_h, delta_, delta_v):
    r, g, b, a = np.rollaxis(arr, axis=-1)
    h, s, v = rgb_to_hsv(r, g, b)
    h += delta_h
    s += delta_s
    v += delta_v
    r, g, b = hsv_to_rgb(h, s, v)
    arr = np.dstack((r, g, b, a))
    return arr

假设您知道原始图像的基色和所需的目标颜色,您可以轻松计算增量:

Assuming you know the base color of your original image and the target color you want, you can easily compute the deltas:

base_h, base_s, base_v = rgb_to_hsv(base_r, base_g, base_b)
target_h, target_s, target_v = rgb_to_hsv(target_r, target_g, target_b)
delta_h, delta_s, delta_v  = target_h - base_h, target_s - base_s, target_v - base_v