0

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.

SirWinning
  • 111
  • 8
  • could we see what code yoou have tried already? – DrBwts Sep 21 '17 at 11:06
  • 1
    I added some of the code which reads image from blender and try to convert it to opencv readable format – SirWinning Sep 21 '17 at 11:52
  • While blender presents colour as float values to the user it is [stored as chars](https://blender.stackexchange.com/a/53140/935). You said your using `shape(w,h,3)` but your code has `shape(w,h,4)` if your image has no alpha then 3 is correct and using 4 will unalign your colour. – sambler Sep 22 '17 at 04:25

0 Answers0