Change image color based on RGB value

Situation:

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

Problem:

There are several different, but similar shades of this color that also need to be converted, which makes the simple β€œchange-all- (0,0,0) -pixels-to- (0,100,200)” solution worthless.

If someone can point me in the right direction regarding an algorithm or image manipulation method that will make this task more manageable, that would be great.

I used PIL to solve this problem, but any general tips would be nice.

edit:

Also, I used this other SO answer ( Change the hue of the image with Python PIL ) to do part of what I ask for (change the hue), but it does not take into account the saturation or value

edit: http://dpaste.org/psk5C/ shows using pil to look at the rgb values ​​I have to work with, and hsv, which are consistent with some of them.

+7
source share
1 answer

Link to the solution in a related question: you can adjust the shift_hue() function to adjust the hue, saturation and value, not just the hue. This should allow you to migrate all of these options as you like.

Original:

 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 

Adjusted Version:

 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 the source image and the target color you need, you can easily calculate the delta:

 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 
+4
source

All Articles