I'm doing a bit of blender scripting atm. I have a python script that works with numpy and opencv to do some image processing and I wanna use that script within blender. The main issue is that when I read an image from blender it comes with pixel values in sRGB. OpenCV is bad with sRGB and therefore I cannot use it properly.
I need a way to convert those sRGB pixels to RGB on the 255 scale or to HEX. I have checked online thoroughly and could not find much relevant to this conversion, apart from sRGB to linear RGB function which only makes the numbers much smaller. Initially I thought that blender stores the pixels in [0,1) range which can be converted to [0,255] by just multiplying, but obv I was wrong as that doesn't work.
Would be nice if somebody familiar with sRGB could explain how you get from those small values to a HEX code or a RGB value on a 255 scale.
Thanks!
Original Image: original pix Some Code Examples: converted to numpy array with shape(w,h,3) also multiplying by 255
img = bpy.data.images.load(finalpath,True)
pixelsImg = np.array(img.pixels)
pixelsImg.shape = (img.size[0],img.size[1],4)
pixelsImg = np.delete(np.dot(pixelsImg,255),3,2)
cv2.imshow('pix2',pixelsImg)
Image now looks like this: after converting to opencv readable format
the function meant to convert sRGB to linear RGB:
def s2lin(x):
a = 0.055
if x <=0.04045 :
y = x * (1.0 / 12.92)
else:
y = pow( (x + a) * (1.0 / (1 + a)), 2.4)
return y
after applying the function:
pixelsImg = np.array(img.pixels)
pixelsImg.shape = (img.size[0],img.size[1],4)
result =np.vectorize(s2lin)
resultList = result(pixelsImg)
cv2.imshow('p',resultList)
cv2.waitKey()
cv2.destroyAllWindows()
The resulting image is fully black.