星期三, 3月 31, 2010

[PIL] Image.composite() 中 mask 的用法

     原始圖檔(Soda.jpg)
藉由演算法找出來的邊緣(Canny.jpg)
 (invertmask.png)
具有彩色線條的邊緣 (result.png)

from PIL import Image, ImageOps, ImageFilter,ImageChops

# the image we want to paste in the transparent mask
background = Image.open("Soda.jpg")

# the mask, where we insert our image
mask = Image.open("Canny.jpg")
invertmask =ImageChops.invert(mask)
invertmask.save("invertmask.png")

whitelayer = Image.new('RGBA',background.size,(255,255,255,0))
whitelayer.save("whitelayer.png")

result = Image.composite(whitelayer,background,invertmask)
result.save("result.png")
result.show()

Following notation are described in document,

Image.composite(i1,i2,mask): return Image
Creates a composite image from two equal-sized images i1 and i2, where mask is a mask image with mode "1", "L", or "RGBA" of the same size.
Each pixel in the output has a value given by (p1 x (1 - m) + p2 x m), where m is the corresponding pixel from mask.

沒有留言: