<p>你在问:</p>
<blockquote>
<p>Is there a more efficient way to do this divide blend operation (less steps and faster)?</p>
</blockquote>
<p>您还可以使用python包<a href="https://pypi.python.org/pypi/blend_modes/" rel="nofollow noreferrer">blend modes</a>。它是用矢量化Numpy数学编写的,通常速度很快。通过<code>pip install blend_modes</code>安装它。我已经用更详细的方式编写了这些命令,以提高可读性,因此链接它们会更短。像这样使用<code>blend_modes</code>分割图像:</p>
<pre><code>from PIL import Image
import numpy
import os
from blend_modes import blend_modes
# Load images
imgA = Image.open('01background.jpg')
imgA = numpy.array(imgA)
# append alpha channel
imgA = numpy.dstack((imgA, numpy.ones((imgA.shape[0], imgA.shape[1], 1))*255))
imgA = imgA.astype(float)
imgB = Image.open('02testgray.jpg')
imgB = numpy.array(imgB)
# append alpha channel
imgB = numpy.dstack((imgB, numpy.ones((imgB.shape[0], imgB.shape[1], 1))*255))
imgB = imgB.astype(float)
# Divide images
imgOut = blend_modes.divide(imgA, imgB, 1.0)
# Save images
imgOut = numpy.uint8(imgOut)
imgOut = Image.fromarray(imgOut)
imgOut.save('PILdiv0.png', 'PNG')
os.system('start PILdiv0.png')
</code></pre>
<p>请注意,要使其工作,两个图像需要具有相同的维度,例如<code>imgA.shape == (240,320,3)</code>和<code>imgB.shape == (240,320,3)</code></p>