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

balck yellow car

Duplicated Image

tutorialspoint

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

car

Duplicated Image

tp logo car
python_pillow_function_reference.htm
Advertisements