Hier hab ich versucht, ein Bild mit der Lichtart D65 einzufärben. Leider bekomme ich jedoch die Fehlermeldung, dass der Datentyp nicht passt. Leider bin ich sehr neu in der Programmiersparche und ohne das deklarieren von Datentypen fällt es mir sehr schwer den Fehler zu beheben. Kann mir vielleicht jemand von euch helfen?
Hier Einmal das Programm:
Code: Alles auswählen
import cv2
import numpy
import PIL
import sys
from PIL import Image
import numpy as np
image_file = "grey_cy.bmp" #imagename
grey_image = Image.open(image_file) #image oeffnen
width, height = grey_image.size
#-----------------------
print "Bildaufloesung:"
print "Breite:", width
print "Hoehe:", height
#-----------------------
color_100 = numpy.array([90,-48,-14]) #Lab-Wert Vollton
color_0 = numpy.array([100,0,0]) #Lab-Wert Hintergrund
referenz_lab2xyz = numpy.array([95.027,100.000,108.883]) #Referenzmatrix Lab nach XYZ
referenz_xyz2rgb = numpy.array(((3.2406,-0.9689,0.0557),(-1.5372,1.8758,0.2040),(-0.4986,0.0415,1.0570))) #Referenzmatrix XYZ nach RGB
back_100 = numpy.array([0,0,0])
back_0 = numpy.array([255,255,255])
M = numpy.array([0,0,0])
N = numpy.array([0,0,0])
def lab2xyz (c,mat):
var_Y = ((c[0]+16)/116)
var_X = ((c[1]/500)+ var_Y)
var_Z = (var_Y - (c[2]/200))
if (var_Y^3)>0.008856:
Y0 = (var_Y)^3
else:
Y0= (((var_Y-16)/116)/7.787)
if (var_X^3)>0.008856:
X0 = (var_X)^3
else:
X0= (((var_X-16)/116)/7.787)
if (var_Z^3)>0.008856:
Z0 = (var_Z)^3
else:
Z0= (((var_Z-16)/116)/7.787)
if (X0*mat[0])>=0:
M[0]= X0*mat[0]
elif (X0*mat[0])>mat[0]:
M[0]=mat[0]
else:
M[0] = 0
if (Y0*mat[1])>=0:
M[1]= Y0*mat[1]
elif (Y0*mat[1])>mat[1]:
M[1]=mat[1]
else:
M[1] = 0
if (Z0*mat[2])>=0:
M[2]= X0*mat[2]
elif (Z0*mat[2])>mat[2]:
M[2]=mat[2]
else:
M[2] = 0
return M
def xyz2rgb(x,mat):
var_X = x[0]/100
var_Y = x[1]/100
var_Z = x[2]/100
var_R = (var_X * mat[0,0]) + (var_Y * mat[0,1]) + (var_Z * mat[0,2])
var_G = (var_X * mat[1,0]) + (var_Y * mat[1,1]) + (var_Z * mat[1,2])
var_B = (var_X * mat[2,0]) + (var_Y * mat[2,1]) + (var_Z * mat[2,2])
if var_R > 0.0031308:
R0 = (1.055 *(var_R**(1.0/2.4))-0.055)
else:
R0 = 12.92 * var_R
if var_G > 0.0031308:
G0 = (1.055 *(var_G**(1/2.4)))-0.055
else:
G0 = 12.92 * var_G
if var_B > 0.0031308:
B0 = (1.055 *(var_B**(1/2.4)))-0.055
else:
B0 = 12.92 * var_B
if (R0 * 255) >= 0:
N[0]= R0*255
elif (R0 * 255) > 255:
N[0]= 255
else:
N[0] = 0
if (G0 * 255) >= 0:
N[1]= G0*255
elif (G0 * 255) > 255:
N[1]= 255
else:
N[1] = 0
if (B0 * 255) >= 0:
N[2]= B0*255
elif (B0 * 255) > 255:
N[2]= 255
else:
N[2] = 0
return N
xyz100 = lab2xyz(color_100,referenz_lab2xyz)
xyz0 = lab2xyz(color_0,referenz_lab2xyz)
rgb100 = xyz2rgb(xyz100,referenz_xyz2rgb)
rgb0 = xyz2rgb(xyz0,referenz_xyz2rgb)
def grey2color(M,x0,x100,r0,r100):
m_R = (r100[0]-r0[0])/(x100[0]-x0[0])
m_G = (r100[1]-r0[1])/(x100[1]-x0[1])
m_B = (r100[2]-r0[2])/(x100[2]-x0[2])
b_R = ((r0[0]*x100[0])-(r100[0]*x0[0]))/(x100[0]-x0[0])
b_G = ((r0[1]*x100[1])-(r100[1]*x0[1]))/(x100[1]-x0[1])
b_B = ((r0[2]*x100[2])-(r100[2]*x0[2]))/(x100[2]-x0[2])
for z in range(0, height, 1):
for s in range(0, width, 1):
A[z][s] = round((( m_R * M[z][s] )+ b_R),0 )
A[z][s+width] = round((( m_G * M[z][s] )+ b_G),0 )
A[z][s+width+width] = round(((m_B * M[z][s] )+ b_B),0 )
return A
image = grey2color(image_file, back_0, back_100, rgb0, rgb100 )
image.save("gcy_out.bmp")