Here are two ways to clone an image with Python:
- Using the Pillow library
- Using the OpenCV library
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 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.
To demonstrate cloning, we will be using the image below:
Method 1: Using the 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.
Install the pillow library if you have not already: pip install pillow
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 the original and cloned images appear the same, but they are actually 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. Since we get the output “False”, it means that both points refer to different objects.
Manipulating a 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 side-by-side comparison of the copy and cloned images above.
We can clearly see that when we rotate the cloned image by 45 degrees, the original image remains unchanged and does not rotate, indicating that 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 the OpenCV library
OpenCV is a computer vision library that provides .copy() method to clone an image from the original image.
It creates a deep copy and does not reflect the changes made to the original image.
The diagram above illustrates each step of the process for cloning 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
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
We obtain the cloned image, which appears identical to the original input image.
We have already explained each step of the above code in the program’s comments.
Verifying the cloned image
Let’s verify the cloned image with the original image and see if it’s the same object or a different object.
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 a cloned image
We will modify the cloned image by rotating it 90 degrees and see if this 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
As shown in the above output image, changes made to the cloned image do not affect the original image.
Conclusion
I would recommend 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.