2010-07-22 68 views
3

如何使用PIL实现与另一个层结合的“躲闪”模式层(如瘸子/ Photoshop中完成)的相同呢?PIL:复合/合并两个图像作为“道奇”

我有我的原始图像以及图像我想与合并图层使用,但我也不怎么办躲闪合并/复合:

from PIL import Image, ImageFilter, ImageOps 

img = Image.open(fname) 

img_blur = img.filter(ImageFilter.BLUR) 
img_blur_invert = ImageOps.invert(img_blur) 

# Now "dodge" merge img_blur_invert on top of img 

回答

7

可能存在做一个纯粹的PIL方式来做到这一点;我不知道。然而,如果没有,这里是一种你可以用numpy做的方法:

import numpy as np 
import Image 
import ImageFilter 

def dodge(front,back): 
    # The formula comes from http://www.adobe.com/devnet/pdf/pdfs/blend_modes.pdf 
    result=back*256.0/(256.0-front) 
    result[result>255]=255 
    result[front==255]=255 
    return result.astype('uint8') 

img = Image.open(fname,'r').convert('RGB') 
arr = np.asarray(img) 
img_blur = img.filter(ImageFilter.BLUR) 
blur = np.asarray(img_blur) 
result=dodge(front=blur, back=arr) 
result = Image.fromarray(result, 'RGB') 
result.show()