PIL, convert RGB to HSV

Code-Stücke können hier veröffentlicht werden.
Antworten
nohhon
User
Beiträge: 1
Registriert: Sonntag 16. November 2008, 15:34

Code: Alles auswählen


import PIL.Image    
import ImageMath

class Converter:
	def __init__(self):
		pass
		
				
	def getHsv(self,Pic):
		Bands = Pic.split()
		Rs = Bands[0]
		Gs = Bands[1]
		Bs = Bands[2]
		
		Buffer = ImageMath.eval("max(r,g)", r=Rs, g=Gs)
		MaxOuts = ImageMath.eval("max(bf,b)", bf=Buffer, b=Bs)
		
		Buffer = ImageMath.eval("min(r,g)", r=Rs, g=Gs)
		MinOuts = ImageMath.eval("min(bf,b)", bf=Buffer, b=Bs)
		
		Diffs = ImageMath.eval("float(x - y)", x=MaxOuts, y=MinOuts)
		Valids = ImageMath.eval("x > 0", x=Diffs)
		
		IsRMaxs = ImageMath.eval("x == y", x=MaxOuts, y=Rs)
		IsGMaxs = ImageMath.eval("x == y", x=MaxOuts, y=Gs)
		IsBMaxs = ImageMath.eval("x == y", x=MaxOuts, y=Bs)
		
		Buffers = ImageMath.eval("x != y", x=IsGMaxs,y=IsRMaxs)
		CleanIsGMaxs = ImageMath.eval("x & y",x=Buffers,y=IsGMaxs)
		
		Alones = ImageMath.eval("x != y", x=IsBMaxs,y=IsRMaxs)
		CleanIsBMaxs = ImageMath.eval("x & y",x=Alones,y=IsBMaxs)
		Alones = ImageMath.eval("x != y", x=CleanIsBMaxs,y=CleanIsGMaxs)
		CleanIsBMaxs = ImageMath.eval("x & y",x=Alones,y=CleanIsBMaxs)
		
		StepSize = "40"
		Offset = "95"
		t = ""
		t += "int("
		t += "Valid * ("
		t += "IsRMax*("+StepSize+"*((g-b)/Diff)+"+Offset+")"
		t += "+CleanIsGMax*("+StepSize+"*(2+(b-r)/Diff)+"+Offset+")"
		t += "+CleanIsBMax*("+StepSize+"*(4+(r-g)/Diff)+"+Offset+")"
		t += ")"
		t += ")"
		#print "t:"
		#print ">%s<"%t
				
		Hues = ImageMath.eval(	 t
								,Valid=Valids
								,Diff=Diffs
								,IsRMax=IsRMaxs
								,CleanIsGMax=CleanIsGMaxs
								,CleanIsBMax=CleanIsBMaxs
								,r=Rs
								,g=Gs
								,b=Bs
								)
		Saturations = ImageMath.eval(
								 "int(Diff/MaxOut*255)"
								,Valid=Valids
								,Diff=Diffs
								,MaxOut=MaxOuts
								)
		Values = MaxOuts
		
		return Hues,Saturations,Values		
	
		
	
	def getHsvSlow(self,Pic):
		StepSize = 40
		Offset = 95
		Size = Pic.size
		Bands = Pic.split()
		Rs = Bands[0].getdata()
		Gs = Bands[1].getdata()
		Bs = Bands[2].getdata()
		Length = Size[0]*Size[1]
		Hs = [0] * Length
		Ss = [0] * Length
		Vs = [0] * Length
		i = 0
		while i < Length:
			r = Rs[i]
			g = Gs[i]
			b = Bs[i]
								#(Section,Max,P,Q,Min)
			B = (
			   (r == g and r == b and (0,r,0,0,r))
			or (r >= g and r >= b and (	   (g >= b and (0,r,g,b,b))
										or (b >  g and (0,r,g,b,g))
										)
				)			
			or (g >= r and g >= b and (	   (r >= b and (2,g,b,r,b))
										or (b >  r and (2,g,b,r,r))
										)			
				)
			or (b >= r and b >= g and (    (r >= g and (4,b,r,g,g))
										or (g >  r and (4,b,r,g,r))
										)			
				)		
			)			
			Diff = float(B[1]-B[4])
			if Diff > 0:
				Hs[i] = int(Offset+StepSize*(B[0]+(B[2]-B[3])/Diff))
				Ss[i] = int(Diff/B[1]*255)
				Vs[i] = B[1]
			else:	 
				Hs[i] = 0
				Ss[i] = 0
				Vs[i] = B[1]
			i += 1 
		HPic = PIL.Image.new("L",Size)
		VPic = PIL.Image.new("L",Size)
		SPic = PIL.Image.new("L",Size)
		HPic.putdata(Hs)
		SPic.putdata(Ss)
		VPic.putdata(Vs)
		
		return HPic,SPic,VPic
		


FullPath = r"Bild.bmp"
Pic = PIL.Image.open(FullPath)

C = Converter()

Hl,Sl,Vl = C.getHsvSlow(Pic)
H,S,V = C.getHsv(Pic)

Redprince
User
Beiträge: 128
Registriert: Freitag 22. Oktober 2004, 09:22
Wohnort: Salzgitter
Kontaktdaten:

Code: Alles auswählen

            B = (
               (r == g and r == b and (0,r,0,0,r))
            or (r >= g and r >= b and (       (g >= b and (0,r,g,b,b))
                                        or (b >  g and (0,r,g,b,g))
                                        )
                )           
            or (g >= r and g >= b and (       (r >= b and (2,g,b,r,b))
                                        or (b >  r and (2,g,b,r,r))
                                        )           
                )
            or (b >= r and b >= g and (    (r >= g and (4,b,r,g,g))
                                        or (g >  r and (4,b,r,g,r))
                                        )           
                )       
            )
Dieser Ausdruck bedarf natürlich keines Kommentares :shock: PEP-8 ist einen Blick wert!
I am not part of the allesburner. I am the [url=http://allesburner.de]allesburner[/url].
Antworten