PIL: 将两张图片合成为“避免混色”的效果

3

如何使用PIL实现将一个图层与另一个图层进行“dodge”模式合并的等效操作(如Gimp / Photoshop中所做的)?

我有原始图像和要与之合并的图层图像,但我不知道如何进行闪避合并/合成:

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
1个回答

8

可能有一种纯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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接