根据RGB值更改图像的颜色 [英] Changing the color of an image based on RGB value

查看:154
本文介绍了根据RGB值更改图像的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您的图像有1种主色,您需要根据给定的rgb值将其转换为其他图像。

You have an image with 1 main color and you need to convert it to another based on a given rgb value.

这种颜色有许多不同但相似的色调也需要转换,这简单' change-all-(0,0,0)-pixels-to-(0,100,200)'解决方案毫无价值。

There are a number of different, but similar shades of that color that also need to be converted, which makes a simple 'change-all-(0,0,0)-pixels-to-(0,100,200)' solution worthless.

如果有人能指出我的方向是正确的一种算法或图像处理技术,可以使这项任务更易于管理,并且很棒。

If anyone can point me in the right direction as far as an algorithm or an image manipulation technique that would make this task more manageable that'd be great.

我一直在使用PIL来尝试这个问题,但任何一般提示都会很好。

I've been using PIL to attempt this problem but any general tips would be nice.

编辑:

另外,我已经使用了这个其他的答案(用Python PIL改变图像色调)做我要求的部分(色调变化) ),但它没有考虑饱和度或值

Also, I've used this other SO answer (Changing image hue with Python PIL) to do part of what I'm asking (the hue change), but It doesn't take into consideration the saturation or value

ed它: http://dpaste.org/psk5C/ 显示使用pil来查看我的rgb值必须和hsv一起工作。

edit: http://dpaste.org/psk5C/ shows using pil to look at the rgb values that I have to work with and the hsv's that go along with some.

推荐答案

参考链接问题中的解决方案:你可以调整 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

这篇关于根据RGB值更改图像的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆