views:

66

answers:

1

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

I have my original image as well as the image I'd like to use as the layer to merge with, but I don't how to do the 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
+4  A: 

There might be a pure-PIL way to do this; I don't know. However, if not, here is a way you could 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()
unutbu