In my image editing app I have a function for converting a 32 bit float image from sRGB to linear color space. The formula is:
if value <= 0.04045: (value / 12.92)
if value > 0.04045: ((value + 0.055) / 1.055)^2.4)
My image is a three-dimensional numpy.ndarray named img32.
My implementation so far:
boolarray = img32 <= 0.04045
lower = (img32 / 12.92) * boolarray.astype(np.int)
upper = np.power(((img32 + 0.055) / 1.055), 2.4) * np.invert(boolarray).astype(np.int)
img32 = lower + upper
So, I am creating a new array boolarray with truth values for <= 0.04045 and multiply by that.
What would be a better solution?
I tried something like:
img32[img32 < 0.04045] = img32 / 12.92
which works on the first step but fails on the second:
img32[img32 >= 0.04045] = np.power(((img32 + 0.055) / 1.055), 2.4)
probably because it doesn't work when wrapped in the np.power function
Any help is appreciated.