Python PIL-函数划分混合两个图像? [英] Python PIL - function to divide blend two images?
问题描述
编辑:感谢Mark和zephyr,代码现在可以正常工作了.zephyr在下面还有两个替代的工作解决方案.
Code is working now, thanks to Mark and zephyr. zephyr also has two alternate working solutions below.
我想用PIL划分两个图像.我找到了 ImageChops.multiply(image1,image2)
,但找不到类似的 divide(image,image2)
函数.
I want to divide blend two images with PIL. I found ImageChops.multiply(image1, image2)
but I couldn't find a similar divide(image, image2)
function.
说明了混合混合模式" (我在这里使用前两个图像作为测试源.)
Divide Blend Mode Explained (I used the first two images here as my test sources.)
是否有我错过的内置除法混合功能(PIL或其他方式)?
Is there a built-in divide blend function that I missed (PIL or otherwise)?
我下面的测试代码正在运行,并且已经接近我要寻找的代码.生成的图像输出类似于此处的除法混合示例图像:
My test code below runs and is getting close to what I'm looking for. The resulting image output is similar to the divide blend example image here: Divide Blend Mode Explained.
是否有一种更有效的方法来执行除法混合操作(步骤更少,速度更快)?最初,我尝试在 Image.eval
和 ImageMath.eval
中使用lambda函数检查黑色像素并将其在分割过程中翻转为白色,但是我无法都能得到正确的结果.
Is there a more efficient way to do this divide blend operation (less steps and faster)? At first, I tried using lambda functions in Image.eval
and ImageMath.eval
to check for black pixels and flip them to white during the division process, but I couldn't get either to produce the correct result.
固定代码,由于Mark和zephyr而缩短了代码.最终的图像输出与以下zephyr的numpy和scipy解决方案的输出相匹配.
Fixed code and shortened thanks to Mark and zephyr. The resulting image output matches the output from zephyr's numpy and scipy solutions below.
# 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')
推荐答案
除法函数在此处有数学定义: http://www.linuxtopia.org/online_books/graphics_tools/gimp_advanced_guide/gimp_guide_node55_002html.
There is a mathematical definition for the divide function here: http://www.linuxtopia.org/online_books/graphics_tools/gimp_advanced_guide/gimp_guide_node55_002.html
这是一个使用scipy/matplotlib的实现:
Here's an implementation with 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)
如果您不想使用matplotlib,则可以这样做(我假设您有numpy):
If you don't want to use matplotlib, you can do it like this (I assume you have 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*ones(shape(c))*(c > 255)
e = d.astype('uint8')
imgOut = Image.fromarray(e)
imgOut.save('PILdiv0.png', 'PNG')
这篇关于Python PIL-函数划分混合两个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!