使用img.split()将波段除以另一波段

时间:2013-09-10 13:24:55

标签: python merge split python-imaging-library

我有一个24位jpeg图片,我试图操纵。我想将红色波段除以绿色波段(然后从0到255标准化)。然后将带与g = 0和b = 0合并。我尝试过使用img.split()和img.merge()方法,但它不起作用。最后我还想把它改成8但是。我相信img.convert('L')会做到这一点。有人可以帮助我吗?

import Image

img = Image.open("M10.jpg")
img.convert("RGB") 

r,g,b=img.split()
p=r/g
g=g.point(lambda i:i*0)
b=b.point(lambda i:i*0)
out=Image.merge('RGB',(p,g,b))
out.show()

1 个答案:

答案 0 :(得分:0)

import Image

#load image, get bands and pixel arrays
img = Image.open('sample.jpg')
r,g,b = img.split()
r_array = r.load()
g_array = g.load()
b_array = b.load()
x,y = r.size

#divide red band by green band (+1 to prevent divide by 0)
for i in xrange(x):
  for j in xrange(y):
    r_array[i, j] /= (g_array[i, j] + 1)

#normalize red band, set green and blue to 0
scalar = 255.0 / r.getextrema()[1]
for i in xrange(x):
  for j in xrange(y):
    r_array[i, j] *= scalar
    g_array[i, j] = 0 
    b_array[i, j] = 0 

#merge the bands
img = Image.merge('RGB', (r, g, b)) 
img = img.convert("P", palette=Image.ADAPTIVE, colors=8)
img.show()

我不确定这是否是你想要完成的,希望它有所帮助。