2010-07-22 66 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()