Vectorizing sRGB to linear conversion properly in Numpy
<p>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:</p>

<pre><code>if value &lt;= 0.04045: (value / 12.92)
if value &gt; 0.04045: ((value + 0.055) / 1.055)^2.4)
</code></pre>

<p>My image is a three-dimensional <strong>numpy.ndarray</strong> named <strong>img32</strong>.</p>

<p>My implementation so far:</p>

<pre><code>boolarray = img32 &lt;= 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
</code></pre>

<p>So, I am creating a new array <strong>boolarray</strong> with truth values for &lt;= 0.04045 and multiply by that.</p>

<p>What would be a better solution?</p>

<p>I tried something like:</p>

<pre><code>img32[img32 &lt; 0.04045] = img32 / 12.92
</code></pre>

<p>which works on the first step but fails on the second:</p>

<pre><code>img32[img32 &gt;= 0.04045] = np.power(((img32 + 0.055) / 1.055), 2.4)
</code></pre>

<p>probably because it doesn't work when wrapped in the <strong>np.power</strong> function</p>

<p>Any help is appreciated.</p>



<pre><code>b = (img32 &lt; 0.04045)
img32[b] /= 12.92

not_b = numpy.logical_not(b)
img32[not_b] += 0.05
img32[not_b] /= 1.055
img32[not_b] **= 2.4
</code></pre>

python numpy vectorization color-space srgb