Python Pillow - ImageChops.lighter() Function



In the Python image processing library Pillow (PIL), the lighter function, located in the ImageOps module, provides a convenient way to perform the comparison of two input images pixel by pixel to get a new image containing the lighter values from each corresponding pair of pixels.

The operation is defined as follows −

$$\mathrm{out\:=\:max(image1,image2)}$$

Syntax

Following is the syntax of the function −

PIL.ImageChops.lighter(image1, image2)

Parameters

Here are the details of this function parameters −

  • image1 − The first input image.

  • image2 − The second input image.

Return Value

The return type of this function is an Image.

Examples

Example 1

In this example, the ImageChops.lighter() function is applied to two random RGB images (image1 and image2) to create a new image containing the lighter values among the two input images. The pixel values of the two input images and the output image at a specified location are observed using the getpixel() function.

from PIL import Image, ImageChops
import numpy as np

# Create two random RGB images
image1 = Image.fromarray(np.array([(35, 64, 3), (255, 0, 0), (255, 255, 0), (255, 255, 255), (164, 0, 3)]), mode="RGB")
print("Pixel values of image1 at (0, 0):", image1.getpixel((0, 0)))

image2 = Image.fromarray(np.array([(25, 14, 3), (25, 222, 0), (255, 155, 0), (255, 55, 100), (180, 0, 78)]), mode="RGB")
print("Pixel values of image2 at (0, 0):", image2.getpixel((0, 0)))

# Get the lighter values of the two images
result = ImageChops.lighter(image1, image2)
print("Pixel values of the result at (0, 0) after lighter:", result.getpixel((0, 0)))

Output

Pixel values of image1 at (0, 0): (35, 0, 0)
Pixel values of image2 at (0, 0): (25, 0, 0)
Pixel values of the result at (0, 0) after lighter: (35, 0, 0)

Example 2

In this example, ImageChops.lighter() function is used to compare the pixel values of two input image(JPEG) pixels to select the lighter values for each corresponding pixel pair.

from PIL import Image, ImageChops

# Open two image files
image1 = Image.open('Images/Tajmahal_2.jpg')
image2 = Image.open('Images/black-doted-butterflies.jpg')

# Compare the two images pixel by pixel and get the lighter values
result = ImageChops.lighter(image1, image2)

# Display the input and resulting images
image1.show()
image2.show()
result.show()

Output

Input Image 1

tajmahal and birds

Input Image 2

dotted butterfly

Output Image

imagechops lighter

Example 3

Here is an example that uses the ImageChops.lighter() function on two PNG image files to get new images with lighter values.

from PIL import Image, ImageChops

# Open two image files
image1 = Image.open('Images/pillow-logo.png')
image2 = Image.open('Images/test_img.png')

# Compare the two images pixel by pixel and get the lighter values
result = ImageChops.lighter(image1, image2)

# Display the input and resulting images
image1.show()
image2.show()
result.show()

Output

Input Image 1

pillow logo s

Input Image 2

test img

Output Image

chops lighter
python_pillow_function_reference.htm
Advertisements