Combining two images into one image is a commonly used operation in image processing. The python image processing library PIL provides a variety of interfaces for combining two images into one image.
Below we combine the two images into one image in different ways.
1. Use the Image.blend() interface
code show as below:
from PIL import Image def blend_two_images(): img1 = Image.open( "bridge.png ") img1 = img1.convert('RGBA') img2 = Image.open( "birds.png ") img2 = img2.convert('RGBA') img = Image.blend(img1, img2, 0.3) img.show() img.save( "blend.png") return
When two images are merged, follow the formula: blended_img = img1 * (1 – alpha) + img2* alpha .
The synthetic results are as follows:
2. Use the Image.composite() interface
This interface uses the form of a mask to merge two images.
code show as below:
def blend_two_images2(): img1 = Image.open( "bridge.png ") img1 = img1.convert('RGBA') img2 = Image.open( "birds.png ") img2 = img2.convert('RGBA') r, g, b, alpha = img2.split() alpha = alpha.point(lambda i: i>0 and 204) img = Image.composite(img2, img1, alpha) img.show() img.save( "blend2.png") return
The effect of 204 specified in line 9 of the code is similar to 0.3 when using the blend() interface.
The combined effect is as follows:
2017.05.09