Calculation of Whitebalance

Hi folks!
I am trying to understand how to get the whitebalance for an image programmatically and having a hard time understanding how to do so.

Can anyone point me towards the function / class in the repo for rawtherapee on the module that calculates the whitebalance for a given raw image?

Thanks, and apologies for the noob question!

Its in the meta data of the image or are you wanting to analyse the image in someway…


Here is the ground up calc’s for the whole image including WB…

1 Like

Thanks for the link! I am trying to get the whitebalance for the raw image.
I have tried looking into the metadata but can’t seem to find it there. I also tried importing the image in Lightroom and exploring the lightroom catalog database, but it doesn’t seem to store it there either :thinking:

I noticed that Rawtherapee also calculates the whitebalance of the image and was wondering where in the source code is the calculation outlined. I tried searching the repo, but having a hard time figuring it out

What do you intend to learn from it? For example, do you already know what white balancing actually means, and that the temperature and tint values you see in software are for a large part meaningless?

1 Like

Are you asking how to go from a temperature and tint to rgb multipliers ? Or howto apply those multipliers ? The latest is easy. The first are multiple methods as far as i know. If only because different raw converters can show different temperatures for the same white balance .

First of all excuse my very bad english.

White balance is a complex thing and there are many approaches. From my point of view, the algorithms present in Rawtherapee work very well in the majority of cases.
However, if the illuminant is multiple - for example daylight and another source - the problem is almost insoluble, especially with fluorescent and LED illuminants. In the latter case, it is possible to make local adjustments by changing (with local adjustments) the chromatic adaptation (Color appearance (Cam16 & Jzczhz) - chromatic adaptation) or (Vibrance & Warm-cool – Warm-cool)

The summary that is present in Rawpedia synthesizes the various possible approaches,in particular the principle of the “auto” algorithm by « temperature correlation »



Now, while the topic of white balance is complex, the fundamental concept is not. Firstoff, white balance really isn’t about the color temperature of the light, that number can only be reliably measured at the scene, so when you take your picture and walk away that number becomes a contrivance. “White Balance” itself is simply about making a thing in the image that’s supposed to be white, actually look white.

So, using the simple 0-255 range of 8-bit JPEGs, what you want is for a pixel from a known-white part of the scene to be where the red, green and blue channels are all 255. So, if you take that pixel from the image and R=200, G=255, and B=180, then you know the channels need to be modified to make R=G=B=255. Typically G is left alone and the R and B channels are multiplied to skew them to G. So, for my arbitrary example the R multiplier would be 255/200=1.275 and the B multiplier would be 255/180=1.417. That’s the essential calculation of white balance.

In the image metadata, the camera will record it’s white balance assertion as multipliers, but it’s not so obvious that’s what they are. My Nikon D7000 delivers a tag that looks like this:

[MakerNotes] WB_GRBGLevels: 256 537 337 256

So, note the G values are on the ends, and the actual multiplier is calculated from these numbers like this, 537/256=2.097 for R, and 337/256=1.316 for B.

I’ve noticed some cameras use 1024 as the green reference.

Like I said, color temperature after you leave the scene is a contrivance. The Kelvin number can be somewhat divined from the multipliers, but the equation is discontinuous and not always close to the scene measurement. This is complicated by what @jdc points out, scenes that are illuminated by different lighting will be impossible to accurately characterize by a single set of multipliers, much less a translation to a color temperature. The mainstream softwares give you such a number because that’s how you think of the light, but really, you’re dealing with the encoded image. Adobe’s DNG processing uses color temperature (dual-illuminant color profiles) to determine a color transform for the camera and the scene it recorded, but you have to have a really good temp number to use it to full effect.

Hope this helps. Welcome to the forum; you’ve come to a good place to learn such; I speak from direct experience.

1 Like