Python Pillow - ImageChops.darker() 函数



PIL.ImageChops.darker 函数逐像素比较两个输入图像,并返回一个新图像,其中包含每个对应像素对中较暗的值。

操作定义如下:

$$\mathrm{Out\:=\:min(image1,image2)}$$

语法

以下是函数的语法:

PIL.ImageChops.darker(image1, image2)

参数

以下是此函数参数的详细信息:

  • image1 - 第一个输入图像。

  • image2 - 第二个输入图像。

返回值

此函数的返回类型为 Image。

示例

示例 1

在此示例中,ImageChops.darker() 函数应用于两个随机的 RGB 图像(image1 和 image2),以创建一个新图像,其中包含两个输入图像中较暗的值。在这里,我们将使用 getpixel() 函数观察两个输入图像以及输出图像在指定位置的像素值。

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 darker values of the two images
result = ImageChops.darker(image1, image2)
print("Pixel values of the result at (0, 0) after darker:", result.getpixel((0, 0)))

输出

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 darker: (25, 0, 0)

示例 2

在此示例中,ImageChops.darker() 函数用于比较两个输入像素的像素值,以选择每个对应像素对中较暗的值。

from PIL import Image, ImageChops

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

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

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

输出

输入图像 1

black rose

输入图像 2

dotted butterfly

输出图像

flower butterfly

示例 3

这是一个使用 ImageChops.darker() 函数对两个 PNG 图像文件获取具有较暗值的新图像的示例。

from PIL import Image, ImageChops

# Open two image files
image1 = Image.open('Images/dark_img1.png')
image2 = Image.open('Images/dark_img2.png')

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

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

输出

输入图像 1

dark img1

输入图像 2

dark_img2

输出图像

dark resultant image
python_pillow_function_reference.htm
广告

© . All rights reserved.