Python PIL - 分割混合两个图像的功能?

mos*_*ski 7 python overlay blend image-processing python-imaging-library

编辑:感谢马克和西风,代码现在正在运作.西风还有以下两种替代工作方案.

我想用PIL分割混合两个图像.我发现ImageChops.multiply(image1, image2)但我找不到类似的divide(image, image2)功能.

分割混合模式解释(我在这里使用前两个图像作为我的测试源.)

是否有我错过的内置分频混合功能(PIL或其他)?

我的测试代码在下面运行,并且正在接近我正在寻找的内容.生成的图像输出类似于此处的除法混合示例图像:分割混合模式说明.

是否有更有效的方法来进行分割混合操作(更少步骤和更快)?起初,我尝试使用lambda函数Image.evalImageMath.eval检查黑色像素及在分裂过程中它们翻转为白色,但我不能让任何产生正确的结果.

编辑:固定代码和缩短感谢马克和西风.得到的图像输出与下面的西风的numpy和scipy解决方案的输出相匹配.

# PIL Divide Blend test

import Image, os, ImageMath

imgA = Image.open('01background.jpg')
imgA.load()
imgB = Image.open('02testgray.jpg')
imgB.load()

# split RGB images into 3 channels
rA, gA, bA = imgA.split()
rB, gB, bB = imgB.split()

# divide each channel (image1/image2)
rTmp = ImageMath.eval("int(a/((float(b)+1)/256))", a=rA, b=rB).convert('L')
gTmp = ImageMath.eval("int(a/((float(b)+1)/256))", a=gA, b=gB).convert('L')
bTmp = ImageMath.eval("int(a/((float(b)+1)/256))", a=bA, b=bB).convert('L')

# merge channels into RGB image
imgOut = Image.merge("RGB", (rTmp, gTmp, bTmp))

imgOut.save('PILdiv0.png', 'PNG')

os.system('start PILdiv0.png')
Run Code Online (Sandbox Code Playgroud)

so1*_*311 3

这里有除法函数的数学定义: http://www.linuxtopia.org/online_books/graphics_tools/gimp_advanced_guide/gimp_guide_node55_002.html

这是 scipy/matplotlib 的实现:

import numpy as np
import scipy.misc as mpl

a = mpl.imread('01background.jpg')
b = mpl.imread('02testgray.jpg')

c = a/((b.astype('float')+1)/256)
d = c*(c < 255)+255*np.ones(np.shape(c))*(c > 255)

e = d.astype('uint8')

mpl.imshow(e)
mpl.imsave('output.png', e)
Run Code Online (Sandbox Code Playgroud)

如果你不想使用 matplotlib,你可以这样做(我假设你有 numpy):

imgA = Image.open('01background.jpg')
imgA.load()
imgB = Image.open('02testgray.jpg')
imgB.load()

a = asarray(imgA)
b = asarray(imgB)
c = a/((b.astype('float')+1)/256)
d = c*(c < 255)+255*个(形状(c))*(c > 255)
e = d.astype('uint8')

imgOut = Image.fromarray(e)
imgOut.save('PILdiv0.png', 'PNG')