Setting color temperature for a given image (like

2020-05-19 10:03发布

I've been assigned to set color temperature to a value between 1000 and 10.000 just like in Photoshop. So far I only have been able to find a table that shows the values here I tried to find the same function in Gimp (since it is open source and I would learn by reading the code) but there is no direct equivalent of it. I have to choose color levels and bend some curves. I've been told that customer want to set the values just like in Photoshop. Can anybody point in the right direction to create the scale?

Edit: Link corrected.

1条回答
我欲成王,谁敢阻挡
2楼-- · 2020-05-19 10:22

You've already found a table of RGB equivalents for each color temperature, but you didn't say where so I found my own: http://www.vendian.org/mncharity/dir3/blackbody/

A natural scene reflects light proportionately to the color of the light that strikes it. This means a simple linear translation should produce the desired effect. If we assume that the existing image is already white balanced so that pure white is (255,255,255) then it's just a matter of multiplying each of the r,g,b values at each pixel by the proportions corresponding to a color temperature.

Here's sample code in Python. The multiplication is done indirectly with a matrix.

from PIL import Image

kelvin_table = {
    1000: (255,56,0),
    1500: (255,109,0),
    2000: (255,137,18),
    2500: (255,161,72),
    3000: (255,180,107),
    3500: (255,196,137),
    4000: (255,209,163),
    4500: (255,219,186),
    5000: (255,228,206),
    5500: (255,236,224),
    6000: (255,243,239),
    6500: (255,249,253),
    7000: (245,243,255),
    7500: (235,238,255),
    8000: (227,233,255),
    8500: (220,229,255),
    9000: (214,225,255),
    9500: (208,222,255),
    10000: (204,219,255)}


def convert_temp(image, temp):
    r, g, b = kelvin_table[temp]
    matrix = ( r / 255.0, 0.0, 0.0, 0.0,
               0.0, g / 255.0, 0.0, 0.0,
               0.0, 0.0, b / 255.0, 0.0 )
    return image.convert('RGB', matrix)

Here's a couple of samples. The first is the original, followed by the outputs at 2500K and 9500K.

Original 2500K 9500K

查看更多
登录 后发表回答