- Python Pillow Tutorial
- Python Pillow - Home
- Python Pillow - Overview
- Python Pillow - Environment Setup
- Basic Image Operations
- Python Pillow - Working with Images
- Python Pillow - Resizing an Image
- Python Pillow - Flip and Rotate Images
- Python Pillow - Cropping an Image
- Python Pillow - Adding Borders to Images
- Python Pillow - Identifying Image Files
- Python Pillow - Merging Images
- Python Pillow - Cutting and Pasting Images
- Python Pillow - Rolling an Image
- Python Pillow - Writing text on image
- Python Pillow - ImageDraw Module
- Python Pillow - Concatenating two Images
- Python Pillow - Creating Thumbnails
- Python Pillow - Creating a Watermark
- Python Pillow - Image Sequences
- Python Pillow Color Conversions
- Python Pillow - Colors on an Image
- Python Pillow - Creating Images With Colors
- Python Pillow - Converting Color String to RGB Color Values
- Python Pillow - Converting Color String to Grayscale Values
- Python Pillow - Change the Color by Changing the Pixel Values
- Image Manipulation
- Python Pillow - Reducing Noise
- Python Pillow - Changing Image Modes
- Python Pillow - Compositing Images
- Python Pillow - Working with Alpha Channels
- Python Pillow - Applying Perspective Transforms
- Image Filtering
- Python Pillow - Adding Filters to an Image
- Python Pillow - Convolution Filters
- Python Pillow - Blur an Image
- Python Pillow - Edge Detection
- Python Pillow - Embossing Images
- Python Pillow - Enhancing Edges
- Python Pillow - Unsharp Mask Filter
- Image Enhancement and Correction
- Python Pillow - Enhancing Contrast
- Python Pillow - Enhancing Sharpness
- Python Pillow - Enhancing Color
- Python Pillow - Correcting Color Balance
- Python Pillow - Removing Noise
- Image Analysis
- Python Pillow - Extracting Image Metadata
- Python Pillow - Identifying Colors
- Advanced Topics
- Python Pillow - Creating Animated GIFs
- Python Pillow - Batch Processing Images
- Python Pillow - Converting Image File Formats
- Python Pillow - Adding Padding to an Image
- Python Pillow - Color Inversion
- Python Pillow - M L with Numpy
- Python Pillow with Tkinter BitmapImage and PhotoImage objects
- Image Module
- Python Pillow - Image Blending
- Python Pillow Useful Resources
- Python Pillow - Quick Guide
- Python Pillow - Function Reference
- Python Pillow - Useful Resources
- Python Pillow - Discussion
Python Pillow - ImageChops.duplicate() Function
The PIL.ImageChops.duplicate() function is an alias for PIL.Image.Image.copy() method. Both perform the same action, they create a duplicate (copy) of the input image. The purpose of this duplication is to allow modifications to the copied image while retaining the original image unchanged.
Syntax
Following is the syntax of the function −
PIL.ImageChops.duplicate(image)
Or
PIL.Image.Image.copy()
Parameters
Here are the details of its parameters −
Image − Image object to create a copy.
Return Value
The function returns an Image object, which is a duplicate (copy) of the input image.
Examples
Example 1
This example demonstrates the process of duplicating or copying an image in two different ways: using the ImageChops.duplicate() function and using the copy() method.
from PIL import Image, ImageChops # Open the original image original_image = Image.open('Images/Car_2.jpg') # Duplicate the original image using the ImageChops.duplicate() function duplicated_image = ImageChops.duplicate(original_image) # Duplicate the original image using the copy() method image_copy = original_image.copy() # Print the IDs of the original and duplicated images print('IDs of the Image objects') print('Original Image:', id(original_image)) print('Duplicated Image:',id(duplicated_image)) print('Copied Image:', id(image_copy))
Output
IDs of the Image objects Original Image: 1568872675552 Duplicated Image: 1568861890592 Copied Image: 1568861889968
You can see that both methods create independent copies of the original image, each with a unique identity.
Example 2
The following example demonstrates how to use the ImageChops.duplicate() method to create a duplicate of an image and then paste another image onto the duplicated one without affecting the original, and then display both images for comparison.
from PIL import Image, ImageChops # Open the original image and logo image = Image.open('Images/Car_2.jpg') logo = Image.open('Images/tutorialspoint2.jpg') # Duplicate the original image image_copy = ImageChops.duplicate(image) # Get the position to paste the logo position = ((image_copy.width - logo.width), (image_copy.height - logo.height)) # Paste the logo onto the duplicated image image_copy.paste(logo, position) # Display the duplicated and original images image_copy.show() image.show()
Output
Original Image
Duplicated Image
You can observe that the original image remains unchanged even after making modifications to the duplicate.
Example 3
The following example demonstrates how to use the Image.copy() method to create a duplicate of an image and then paste another image onto the duplicated one without affecting the original. Afterward, both images are displayed for comparison.
from PIL import Image, ImageDraw # Open the original image and logo original_image = Image.open('Images/Car_2.jpg') logo = Image.open('Images/tutorialspoint2.jpg') # Duplicate the original image duplicated_image = original_image.copy() # Get the position to paste the logo position = ((duplicated_image.width - logo.width), (duplicated_image.height - logo.height)) # Paste the logo onto the duplicated image duplicated_image.paste(logo, position) # Display both the duplicated and original images duplicated_image.show() original_image.show()
Output
Original Image
Duplicated Image