What do you understand when people ask you to clone an image? Well, let me tell you.
Cloning an image means creating an exact copy of an image object. After cloning, when you edit that cloned image object, it should not modify the original image. This cloning process can be helpful in various scenarios where you have to preserve the original image and edit the cloned image.
Although you can use “clone” and “copy” interchangeably. However, In programming, copy simply means shallow copy, and clone means deep copy as far as general understanding goes!
In shallow copy, if you change the copied image, it will modify the original image.
In deep copy, if you change the copied image, it won’t change the original image.
In this article, we will clone an image which will be an exact copy of the original image, and then modify the cloned image and check if the original image is being changed or not.
Here are two ways to clone an image with Python:
- Using Pillow library
- Using OpenCV library
To demonstrate cloning, we will be using the image below:
Method 1: Using Pillow library
The “Pillow” is the successor of the “Python Imaging Library (PIL)” that is widely used for image manipulation.
First, load an image using Image.load() method and clone the image using .copy() method. And finally, display the cloned image.
Decision Tree Diagram
Install the pillow library if you have not already:
pip install pillow
Code example
from PIL import Image # Opens the image and returns an image object source_image = Image.open('krunal.jpg') # Cloning the image object cloned_image = source_image.copy() # Displaying copied/cloned image cloned_image.show()
Output of cloned image
You can see that both original and cloned images look the same but they are different images.
Verifying the cloned image
To check if both images are different, we can use the “is” operator in Python.
from PIL import Image # Opens the image and returns an image object source_image = Image.open('krunal.jpg') # Cloning the image object cloned_image = source_image.copy() # Checking if source_image is the same as cloned_image print(source_image is cloned_image)
Output
False
The “is” operator checks if both image objects point to the same object. And since we get the output “False”, that means both points to different objects.
Manipulating cloned image
Let’s manipulate the “cloned image” to check if the original image is also changing or not.
from PIL import Image # Opens the image and returns an image object source_image = Image.open('krunal.jpg') # Cloning the image object cloned_image = source_image.copy() # Rotating cloned image to 45 degrees cloned_image = cloned_image.rotate(45) # Displaying original and cloned images source_image.show() cloned_image.show()
Output
You can see the above side-by-side comparison of copy and cloned images. We can clearly see that when we rotate the cloned image to 45 degrees, the original image does not rotate and stays as it is which means a deep copy has been created.
The Pillow is a simple and Pythonic approach that provides basic image processing functions. It supports various image formats. However, it can be slower compared to OpenCV.
Method 2: Using OpenCV library
The OpenCV is a computer vision library that provides .copy() method to clone an image from the original image. It will create a deep copy and does not reflect the changes to the original image.
Decision Tree Diagram
The above diagram depicts each step of the process to clone an image using the cv2 module. You need to import the opencv-python library as cv2 and use its methods.
Install the opencv-python library if you have not already:
pip install opencv-python
Code example
import cv2 # Loading the original image source_image = cv2.imread('krunal.jpg') # Cloning the image duplicate_image = source_image.copy() # Displaying the image cv2.imshow('Cloned Image', duplicate_image) # When a user hits the "0" key, # It will close the display window cv2.waitKey(0) cv2.destroyAllWindows()
Output of cloned image
And we get the cloned image that looks exactly the same as the original input image.
We already explained each step of the above code in the comments of the program.
Verifying the cloned image
Let’s verify the cloned image with the original image and see if it’s same object or different objects.
import cv2 # Loading the original image source_image = cv2.imread('krunal.jpg') # Cloning the image duplicate_image = source_image.copy() # Using is operator to check # If source_image and duplicated_image are same print(source_image is duplicate_image)
Output
False
That means both refer to different objects.
Manipulating cloned image
We will modify the cloned image and rotate it to 90 degrees and see if it affects the original image as well.
import cv2 # Loading the original image source_image = cv2.imread('krunal.jpg') # Cloning the image duplicate_image = source_image.copy() # Rotating the cloned image (Manipulating cloned image) cloned_image = cv2.rotate(duplicate_image, cv2.ROTATE_90_CLOCKWISE) # Displaying both images cv2.imshow('Original Image', source_image) cv2.imshow('Cloned Image', cloned_image) cv2.waitKey(0) cv2.destroyAllWindows()
Output
You can see from the above output image that changing in the cloned image does not modify the original image.
Conclusion
I would recommend for using the OpenCV approach because it is optimized for performance and has excellent support for image and video processing. Furthermore, you can apply transformations to copies of the images without altering the source.
That’s all for the day! Feel free to ask any questions in the comment section.