Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- # Assuming `xyz` is your input ndarray of shape (600, 800, 3)
- xyz = np.array(xyz, dtype=np.float32) # Ensure the array is in float
- # Normalize if not already normalized
- xyz /= np.max(xyz)
- # White point adaptation matrix from D65 to D50
- M_D65_to_D50 = np.array([
- [ 0.9555766, -0.0230393, 0.0631636],
- [-0.0282895, 1.0099416, 0.0210077],
- [ 0.0122982, -0.0204830, 1.3299098]
- ])
- # Reshape xyz to (n, 3) for matrix multiplication
- xyz_flat = xyz.reshape(-1, 3)
- # Apply the white point adaptation
- xyz_d50_flat = np.dot(xyz_flat, M_D65_to_D50.T)
- # Reshape back to original shape
- xyz_d50 = xyz_d50_flat.reshape(xyz.shape)
- # Transformation matrix from XYZ (D50) to linear RGB
- M_XYZ_to_RGB = np.array([
- [ 3.2404542, -1.5371385, -0.4985314],
- [-0.9692660, 1.8760108, 0.0415560],
- [ 0.0556434, -0.2040259, 1.0572252]
- ])
- # Apply the transformation to get linear RGB
- rgb_linear_flat = np.dot(xyz_d50_flat, M_XYZ_to_RGB.T)
- # Reshape back to original shape
- rgb_linear = rgb_linear_flat.reshape(xyz_d50.shape)
- # Gamma correction function
- def gamma_correction(channel):
- return np.where(channel <= 0.0031308,
- 12.92 * channel,
- 1.055 * np.power(channel, 1/2.4) - 0.055)
- # Apply gamma correction
- rgb_srgb = gamma_correction(rgb_linear)
- # Clip values to the range [0, 1]
- rgb_srgb = np.clip(rgb_srgb, 0, 1)
- # rgb_srgb now contains the sRGB color data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement