Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Superimposition of Images

00:00 Superimposition of Images Using .paste(). Now everything is in place. You can paste the segmented image of the cat into the image of the cloister.

00:10 Open a new REPL session and start by importing the images that you’ll need.

01:00 Here you use .paste() to paste an image onto another one. This method can be used with three arguments. The first argument is the image that you want to paste in.

01:11 You are resizing this image to one-fifth of its original size using the integer division operator (//), commonly known as floor division. The second argument is the location in the main image where you want to paste the second picture.

01:26 The tuple contains the coordinates in the main image where you want to place the top-left corner of the image that you are pasting in. The third argument provides the mask that you wish to use if you don’t want to paste in the entire image. Once again, this is resized using floor division.

01:44 You’ve used the mask that you obtained from the process of thresholding, erosion, and dilation to paste the cat without its background. Finally, you show this image on-screen.

01:56 You’ve segmented the cat from one image and placed it into an image of the cloister to show it sitting there rather than in the field where it was originally taken.

02:06 Your final task in this example is to add the Real Python logo as a watermark to the image. The Real Python logo is in the course materials. Continue working in the same REPL session.

02:20 Start by opening the file and then showing the image on-screen.

02:36 You change the image into grayscale, set a threshold, and then use the .point() method to transform it into a black-and-white image.

02:56 You reduce its size

03:08 and transform it into a contour image.

03:16 Finally, you show the processed image on-screen. To use this as a watermark, you’ll need to reverse the colors so that the background is black and only the outline you want to keep is white.

03:32 You can achieve this by using .point() once more.

03:41 This converts any pixel with a value of 255 to 0, and any other values become 255. Once again, you show the processed logo on-screen to see your progress.

03:57 The final step is to paste this outline onto the image of the cat sat in the cluster. Once again, you use the .paste() method. The first argument in .paste() is the image that you want to paste in (the modified logo), and the second argument provides the top-left coordinates of the region where you want to paste it.

04:20 But here you are using the same image as the mask because the image is a binary image. So this image will now include the Real Python watermark, which you show on-screen.

04:33 The watermark has a rectangular outline, which is a result of the contour filter that you used earlier on. If you prefer to remove the outline, you can crop the image using .crop(), and this is an exercise you can try on your own.

04:47 In the next section of the course, you’ll see how to use the NumPy library along with Pillow to manipulate images.

Become a Member to join the conversation.