加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – PIL:复合/合并两个图像为“道奇”

发布时间:2020-12-20 11:14:34 所属栏目:Python 来源:网络整理
导读:如何使用PIL实现相当于将“闪避”模式中的图层与另一图层合并(如Gimp / Photoshop中所做的那样)? 我有原始图像以及我想用作合并图层的图像,但我不知道怎么做道奇合并/复合: from PIL import Image,ImageFilter,ImageOpsimg = Image.open(fname)img_blur =
如何使用PIL实现相当于将“闪避”模式中的图层与另一图层合并(如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

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读