Changing the color of an image based on RGB value

2024/11/17 20:41:24

Situation:

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

Problem:

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.

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

edit:

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

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.

Answer

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.

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 = houtr, 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_hs += delta_sv += delta_vr, 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
https://en.xdnf.cn/q/71147.html

Related Q&A

Python NumPy - FFT and Inverse FFT?

Ive been working with FFT, and Im currently trying to get a sound waveform from a file with FFT, (modify it eventually), but then output that modified waveform back to a file. Ive gotten the FFT of the…

Tools to help developers reading class hierarchy faster

I mostly spend time on Python/Django and Objective-C/CocoaTouch and js/jQuery in the course of my daily work.My editor of choice is vim for Python/Django and js/jQuery and xcode for Objective-C/CocoaTo…

Python Last Iteration in For Loop [duplicate]

This question already has answers here:What is the pythonic way to detect the last element in a for loop?(34 answers)How do I read and write CSV files?(7 answers)Closed 9 months ago.Is there any simp…

Django 1.7 multisite User model

I want to serve a Django application that serves multiple web sites by single database but different user sets. Think like a blog application, it will be used by several domains with different themes, …

Does for key in dict in python always iterate in a fixed order?

Does the python codefor key in dict:..., where dict is a dict data type, always iterate in a fixed order with regrard to key? For example, suppose dict={"aaa":1,"bbb",2}, will the …

Kinesis Firehose lambda transformation

I have the following lambda function as part of Kinesis firehose record transformation which transforms msgpack record from the kinesis input stream to json.Lambda Runtime: python 3.6from __future__ im…

Python: find out whether a list of integers is coherent

I am trying to find out whether a list of integers is coherent or at one stretch, meaning that the difference between two neighboring elements must be exactly one and that the numbers must be increasin…

Create resizable/multiline Tkinter/ttk Labels with word wrap

Is it possible to create a multi-line label with word wrap that resizes in sync with the width of its parent? In other words the wordwrap behavior of Notepad as you change the width of the NotePad win…

Unicode, regular expressions and PyPy

I wrote a program to add (limited) unicode support to Python regexes, and while its working fine on CPython 2.5.2 its not working on PyPy (1.5.0-alpha0 1.8.0, implementing Python 2.7.1 2.7.2), both run…

Python str object has no attribute read

Python 3.3.2 import json & urllib.requestJson[{"link":"www.google.com","orderid":"100000222"}, {"link":"www.google.com","orderid&quo…