You can combine, merge, blend or paste images for artistic or practical reasons and achieve ingenious results. You can also come up with creative backgrounds or different textures to add to your digital images.
In this tutorial, we will elaborate different methods for combining images, elaborate transparency settings for merging images and provide plenty of Python examples to communicate these ideas efficiently.
Merging an image means combining parts of two or more images to achieve a composition. Pasting images can be useful for creating presentations, art, special effects and rich compositions. Image pasting is a powerful concept in Python’s PIL library.
Holy Python is reader-supported. When you buy through links on our site, we may earn an affiliate commission.
img.paste(img2, (400,400))
img.paste(img2, (400,400,1200,1200))
from PIL import Image
img = Image.open("/home/zoom_call.jpg")
img2 = Image.open("/home/wind_turbines.png")
img.paste(img2, (400,400))
img.show()
from PIL import Image
img = Image.open("/home/zoom_call.jpg")
img2 = Image.open("/home/wind_turbines.png").resize((2750,1920)).rotate(-2, expand=1)
img.paste(img2, (2450,450))
img.show()
from PIL import Image, ImageDraw
img = Image.open("/home/zoom_call.jpg").convert("RGBA")
img2 = Image.open("/home/wind_turbines.png")
.resize((2750,1920)).rotate(-2, expand=1)
img.paste(img2, (2450,450), mask=img2)
img.show()
So, that’s the final code that’s been used to create the final image. A few important points:
Final result achieved by a few adjustments is nearly perfect and it can all done from the command line environment (cli). Another big advantage of computational photography or more specifically batch editing digital images with Python is that you can scale it to thousands or even millions of images.
Another important component of computational image pasting is the mask argument. Mask is necessary when the image being pasted requires some kind of transparency. (For example, image being pasted is an apple but since all images have a box shape the areas surrounding the apple might have no image data except transparency.)
One thing that’s commonly confused is the usage of mask layer. Mask layer only determines the coordinates of the pixels that are being passed, the actual image being pasted is still the image that’s passed to .paste() method as the first argument.
In summary, coordinates of pasting comes from the mask argument (or mask layer provided by it) and the actual visual that’s being pasted comes from a separate image.
So it’s convenient that in many examples, mask argument and the image being pasted will be the same image. It’s useful when pasting an image to assign the coordinates and the visual of the non-transparent part to be the same image if that’s the goal.
But if you are looking to paste, say an orange in an apple shape this can also be possible in which case mask image will be different than the image being pasted.
Two important properties of the image being passed to the mask argument are as below.
Here we have a Python example that might be even more useful to understand how mask argument works.
from PIL import Image, ImageDraw
img = Image.new("RGBA", (2000,2000))
img2 = Image.open("/home/usa/Downloads/orange_2.jpg").convert("RGBA").convert("RGBA")
x, y = img2.size
img3 = Image.open("/home/usa/Downloads/transparent-apple-21.png").resize((x,y))
img.paste(img2, (0,0), mask=img3)
img.show()
You can use the same technique to create infinite cool combination. A city photo in a car shape, puppy in a heart shape, an oil refinery in a wind turbine shape are just a few other ideas.
Composite method works very similarly to the .paste() method. The main difference is the way they are applied. While .paste() method is applied on the image as an image method, composite works as a function and it needs both images to be passed as arguments inside it. Check out the mini examples below.
img1.paste(img2, (100,100))
Image.composite(img1, img2)
Composite is an ancient word and it signifies physical or digital things which are composed of multiple parts.
In science and technology, composite is a popular and commonly used term such as composite material, composite signal, composite numbers and composite images. For example, space crafts have heat shield made of composite material (such as fibrous refractory which can withstand unimaginable orbit entry conditions. Google Patent)
Composite comes from Latin roots of com (with, together) + posite (position).
Anyway, back to composite method for pasting images. .composite() method has a couple of drawbacks to the previously demonstrated .paste() method.
Disadvantages of composite method:
Python code below creates an image mask in ellipse mode. Check out how to draw geometric shapes on images with Python tutorial to learn more about drawing ellipses, circles and other shapes with Python Image Library (PIL).
from PIL import Image, ImageDraw
img = Image.open("/home/fox.jpg").convert("RGBA")
mask = Image.new("L", img.size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((300, 1500, 1220, 2250), fill=185)
x,y = img.size
img2 = Image.open("/home/crow.jpg").convert("RGBA").resize((x,y))
img3 = Image.composite(img2, img, mask=mask)
img3.show()
Image.blend(img1, img2, alpha)
from PIL import Image
img = Image.open("/home/orange.jpg").convert("RGBA")
x, y = img.size
img2 = Image.new("RGBA", (x,y), color="red")
img3 = Image.blend(img,img2, 0.7)
img3.show()
Below we have a Python example regarding the blend method which shows blending of a new image with plain red color and an orange image using the blend method and various alpha values.
So blend method can be convenient for overlaying images when no transparency is involved. There are pros and cons of this method as following.
Pros of blend:
Cons of blend:
If when using .blend() method, image sizes don’t match you will get an error similar to below.
return im1._new(core.blend(im1.im, im2.im, alpha))
ValueError: images do not match
This error is easy to fix, you just need to resize or crop the images so that they have the same height and width.
Image.alpha_composite(img1, img2)
Important thing is since there is no alpha adjustment parameter or
mask argument, images will need to have appropriate transparency values
beforehand.
RGBA
is a useful color mode that can be applied to images. Most colorful digital images you come across in daily life will either have RGB or RGBA color modes..putalpha
is a very convenient method that can be applied to images with Python’s PIL library.Important thing is since there is no alpha adjustment parameter or
mask argument, images will need to have appropriate transparency values
beforehand.
Below you can see the usage of the alpha_composite method in combination with the .putalpha method which is also explained down below.
from PIL import Image, ImageDraw
img = Image.open("/home/usa/Downloads/leio-mclaren-FwdZYz0yc9g-unsplash.jpg").convert("RGBA")
x,y = img.size
img2 = Image.open("/home/usa/Downloads/arthur-chauvineau-Dn7P1U26ZkE-unsplash.jpg").convert("RGBA").resize((x,y))
img.putalpha(225)
img2.putalpha(45)
img3 = Image.alpha_composite(img, img2)
img3.show()
In this Python Tutorial we learned how to paste, blend, mix and overlay images using different techniques from the Image module of PIL library.
To achieve various image merging results we used Python methods such as .paste(), .composite(), .alpha_composite() and .blend().
We also supportive classes and methods such as RGBA images, .convert() method, .resize() method, .putalpha() method, .size attribute and we made use of the mask argument when available.
You can consider revisiting our basic image editing tutorials such as, resizing images, creating new images, saving images and cropping images.