Combining images by blending

The effect of blending two images is like projecting two transparent slide images onto a projector screen from two separate projectors where the amount of light from each projector is controlled by proportion setting. The command is of the form Image.blend(image_1, image_2, proportion-of-image_1).

Getting ready

Use the two images 100_canary.png and 100_cockcrow.png from the folder /constr/pics1. The 100_ in the titles is a reminder that the images are 100 x 100 pixels in size and we will see the format, size, and type of each image printed onto the console.

How to do it...

With the two identical size and mode images in place, execute the following code.

# image_blend_1.py
# >>>>>>>>>>>>>>>>
import Image
im_1 = Image.open("/constr/pics1/100_canary.png") # mode is RGBA
im_2 = Image.open("/constr/pics1/100_cockcrow.png") # mode is RGB
# Check on mode, size and format first for compatibility.
print "im_1 format:", im_1.format, ";size:", im_1.size, "; mode:",im_1.mode
print "im_2 format:", im_2.format, ";size:", im_2.size, "; mode:",im_2.mode
im_2 = im_2.convert("RGBA") # Make both modes the same
im_4 = Image.blend(im_1, im_2, 0.5)
im_4.show()

There's more...

From format information, we will see that the mode of the first image is RGBA while the second is RGB. Therefore, it is necessary to first convert the second image to RGBA.

In this particular example, the proportion control was set to 0.5. That is, the two images were blended together by equal amounts. If the proportion setting had been 0.2, then 20% of im_1 would have been combined with 80% of im_2.

More Info Section 1

Another way of combining images would be to use a third image as a mask to control the positions where the mask determines both the shape and the proportion of each image in the resulting image.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.138.202