Srgb to linear python

Python – How to convert from sRGB to linear sRGB for computing the color correction matrix in opencv? (CCM)

I have referred to the following stackoverflow thread for computing the color correction matrix.

As referred in the thread mentioned above, I want to convert from the sRGB color space to linear sRGB space, I’m trying to use the pwkit colorspace mapper code for the conversion.

However, i’m unsure about the resulting linear sRGB values, since the function requires sRGB in range 1, is dividing the sRGB values by 255.0, the correct approach? How to verify that the linear sRGB values returned by the function are correct?

import os import numpy as np import cv2 import matplotlib.pyplot as plt def srgb_to_linsrgb (srgb): """Convert sRGB values to physically linear ones. The transformation is uniform in RGB, so *srgb* can be of any shape. *srgb* values should range between 0 and 1, inclusively. """ gamma = ((srgb + 0.055) / 1.055)**2.4 scale = srgb / 12.92 return np.where (srgb > 0.04045, gamma, scale) if __name__ == "__main__": colorChecker = cv2.imread('C:/Users/Ai/Documents/Urine Sample Analysis/Assets/colorchecker_1.jpg') cc = cv2.cvtColor(colorChecker,cv2.COLOR_BGR2RGB) plt.imshow(cc) #Convert srgb to linear rgb cc = cc / 255.0 cc1 = srgb_to_linsrgb(cc) print("Conversion from sRGB to linear RGB:\n") print(cc1[1,1,:]) 

The result of conversion is: [0.30946892 0.23455058 0.19806932]

Читайте также:  Тип данных элементов массива python

The input sRGB should be between 0-1, how to scale the value of sRGB channels from 245 to 1, would simple division by 255.0 result in correct linear sRGB values? How to verify that the conversion has resulted in correct linear sRGB values?

Best Solution

From a terminology standpoint and assuming your image was effectively sRGB encoded, i.e. following IEC 61966-2-1:1999, you are decoding sRGB encoded non-linear light values by applying the sRGB Electro-Optical Transfer Function (EOTF), a type of colour component transfer function (CCTF) thus saying:

I want to convert from the sRGB color space to linear RGB space

is not correct because you are still effectively using sRGB encoded colours, the only difference is that prior to using the decoding CCTF they were non-linearly encoded. An additive RGB colourspace is defined by a set of three components: primaries, whitepoint and CCTFs. By using the CCTF you have not changed at all the primaries or whitepoint, i.e. the gamut is the same, thus what you should say is more along those lines:

I want to convert from the sRGB color space to linear sRGB color space

Now to verify that your approach is correct, you can compare with a reference implementation such as Colour whose code is used in the SO thread you referenced: 18% gray is 118 upon non-linear encoding using sRGB inverse EOTF:

>>> int(round(colour.models.eotf_inverse_sRGB(0.18) * 255)) 118 

From there you can check if your method does convert back to 18% and it does (disregarding rounding)!

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Color converter for Python 3.x. Supported formats: RGB, HSL, XYZ, L*a*b*, L*C*h.

License

senritsuki/Python-color-converter

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Color converter for Python 3.x. Supported formats: RGB, HSL, XYZ, L*a*b*, L*C*h.

import color_converter as cc cc.rgbhex_to_rgb255('#0080ff') # -> [0, 128, 255] cc.rgb255_to_rgbhex((0, 128, 255)) # -> '#0080ff'
Function name Argument value Return value
hsl_to_rgb01 HSL array
(0.0, 0.0, 0.0) .. (360.0, 1.0, 1.0)
RGB array
(0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0)
hsl_to_rgb255 HSL array
(0.0, 0.0, 0.0) .. (360.0, 1.0, 1.0)
RGB array
(0, 0, 0) .. (255, 255, 255)
hsl_to_rgbhex HSL array
(0.0, 0.0, 0.0) .. (360.0, 1.0, 1.0)
RGB string
‘#000000’ .. ‘#ffffff’
lab_to_lch D50 CIE L*a*b* array
(0.0, *.*, *.*) .. (100.0, *.*, *.*)
D50 CIE L*C*h array
(0.0, 0.0, 0.0) .. (100.0, *.*, 360.0)
lab_to_rgb01 D50 CIE L*a*b* array
(0.0, *.*, *.*) .. (100.0, *.*, *.*)
RGB array
(0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0)
lab_to_rgb255 D50 CIE L*a*b* array
(0.0, *.*, *.*) .. (100.0, *.*, *.*)
RGB array
(0, 0, 0) .. (255, 255, 255)
lab_to_rgbhex D50 CIE L*a*b* array
(0.0, *.*, *.*) .. (100.0, *.*, *.*)
RGB string
‘#000000’ .. ‘#ffffff’
lab_to_xyz D50 CIE L*a*b* array
(0.0, *.*, *.*) .. (100.0, *.*, *.*)
D50 CIE XYZ array
(*.*, *.*, *.*) .. (*.*, *.*, *.*)
lch_to_lab D50 CIE L*C*h array
(0.0, 0.0, 0.0) .. (100.0, *.*, 360.0)
D50 CIE L*a*b* array
(0.0, *.*, *.*) .. (100.0, *.*, *.*)
lch_to_rgb255 D50 CIE L*C*h array
(0.0, 0.0, 0.0) .. (100.0, *.*, 360.0)
RGB array
(0, 0, 0) .. (255, 255, 255)
lch_to_rgbhex D50 CIE L*C*h array
(0.0, 0.0, 0.0) .. (100.0, *.*, 360.0)
RGB string
‘#000000’ .. ‘#ffffff’
lrgb_to_srgb D65 linear sRGB array
(0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0)
D65 non linear sRGB array
(0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0)
lrgb_to_xyz D65 linear sRGB array
(0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0)
D50 CIE XYZ array
(*.*, *.*, *.*) .. (*.*, *.*, *.*)
rgb01_to_hsl RGB array
(0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0)
HSL array
(0.0, 0.0, 0.0) .. (360.0, 1.0, 1.0)
rgb01_to_lab RGB array
(0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0)
D50 CIE L*a*b* array
(0.0, *.*, *.*) .. (100.0, *.*, *.*)
rgb01_to_rgb255 RGB array
(0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0)
RGB array
(0, 0, 0) .. (255, 255, 255)
rgb01_to_rgbhex RGB array
(0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0)
RGB string
‘#000000’ .. ‘#ffffff’
rgb255_to_hsl RGB array
(0, 0, 0) .. (255, 255, 255)
HSL array
(0.0, 0.0, 0.0) .. (360.0, 1.0, 1.0)
rgb255_to_lab RGB array
(0, 0, 0) .. (255, 255, 255)
D50 CIE L*a*b* array
(0.0, *.*, *.*) .. (100.0, *.*, *.*)
rgb255_to_lch RGB array
(0, 0, 0) .. (255, 255, 255)
D50 CIE L*C*h array
(0.0, 0.0, 0.0) .. (100.0, *.*, 360.0)
rgb255_to_rgb01 RGB array
(0, 0, 0) .. (255, 255, 255)
RGB array
(0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0)
rgb255_to_rgbhex RGB array
(0, 0, 0) .. (255, 255, 255)
RGB string
‘#000000’ .. ‘#ffffff’
rgbhex_to_hsl RGB string
‘#000000’ .. ‘#ffffff’
HSL array
(0.0, 0.0, 0.0) .. (360.0, 1.0, 1.0)
rgbhex_to_lab RGB string
‘#000000’ .. ‘#ffffff’
D50 CIE L*a*b* array
(0.0, *.*, *.*) .. (100.0, *.*, *.*)
rgbhex_to_lch RGB string
‘#000000’ .. ‘#ffffff’
D50 CIE L*C*h array
(0.0, 0.0, 0.0) .. (100.0, *.*, 360.0)
rgbhex_to_rgb01 RGB string
‘#000000’ .. ‘#ffffff’
RGB array
(0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0)
rgbhex_to_rgb255 RGB string
‘#000000’ .. ‘#ffffff’
RGB array
(0, 0, 0) .. (255, 255, 255)
srgb_to_lrgb D65 non linear sRGB array
(0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0)
D65 linear sRGB array
(0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0)
xyz_to_lab D50 CIE XYZ array
(*.*, *.*, *.*) .. (*.*, *.*, *.*)
D50 CIE L*a*b* array
(0.0, *.*, *.*) .. (100.0, *.*, *.*)
xyz_to_lrgb D50 CIE XYZ array
(*.*, *.*, *.*) .. (*.*, *.*, *.*)
D65 linear sRGB array
(0.0, 0.0, 0.0) .. (1.0, 1.0, 1.0)

About

Color converter for Python 3.x. Supported formats: RGB, HSL, XYZ, L*a*b*, L*C*h.

Источник

Оцените статью