我正在使用Python和PIL(pillow)编写脚本进行绿幕合成。我可以对720p图像进行关键字处理,但是还有一些绿色溢出。我正在编写一个程序来消除这种溢出,但是我发现这个过程非常耗时。我可能可以使用numpy技巧提高速度,但我不太熟悉它。你有什么建议吗?
这是我的去溢出程序。它需要一个PIL图像和一个灵敏度数字,但我目前将其保留在1上...效果很好。对于一个720p的帧,我需要超过4秒来消除这种溢出。相比之下,关键字处理程序每帧运行约2秒钟。
这是我的去溢出程序。它需要一个PIL图像和一个灵敏度数字,但我目前将其保留在1上...效果很好。对于一个720p的帧,我需要超过4秒来消除这种溢出。相比之下,关键字处理程序每帧运行约2秒钟。
def despill(img, sensitivity=1):
"""
Blue limits green.
"""
start = time.time()
print '\t[*] Starting despill'
width, height = img.size
num_channels = len(img.getbands())
out = Image.new("RGBA", img.size, color=0)
for j in range(height):
for i in range(width):
#r,g,b,a = data[j,i]
r,g,b,a = img.getpixel((i,j))
if g > (b*sensitivity):
out_g = (b*sensitivity)
else:
out_g = g
# end if
out.putpixel((i,j), (r,out_g,b,a))
# end for
# end for
out.show()
print '\t[+] done.'
print '\t[!] Took: %0.1f seconds' % (time.time()-start)
exit()
return out
# end despill
我尝试着将输出的像素值写入到一个numpy数组中,然后将该数组转换为PIL图像,而不是使用putpixel方法。但是这种方式平均需要超过5秒钟...所以这个方法更快一些。我知道putpixel可能不是最快的选项,但我确实没有更好的办法...