PIL: Composite / merge two images like "Dodge"

How to use PIL to implement the equivalent of merging a layer in "dodge" mode with another layer (as is done in Gimp / Photoshop)?

I have a source image, as well as an image that I would like to use as a merge layer, but I don't do dodge merge / composite:

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
+3
source share
1 answer

There may be a pure-PIL method for this; I dont know. However, if not, then you can do it with 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()
+7
source

Source: https://habr.com/ru/post/1756052/


All Articles