Python Pillow 简明教程

Python Pillow - Enhancing Color

Enhancing color 是一种图像处理技术,侧重于通过微调其颜色的平衡和饱和度来提高数字图像的视觉吸引力和质量。图像颜色增强的目标是在突出有用信息的同时获得更精细的图像细节。当图像的照明条件较差,导致出现较暗且对比度较低的画面,需要进行细化时,此技术特别有用。此技术广泛用于平面设计、摄影和数字艺术等各个领域,以增强图像的视觉效果。

Enhancing color is an image processing technique that focuses on improving the visual appeal and quality of a digital image by fine-tuning the balance and saturation of its colors. The objective of image color enhancement is to get finer image details while highlighting the useful information. This technique is especially useful in scenarios where images have poor illumination conditions, resulting in darker and low-contrast appearances that require refinement. This technique is widely used in various fields, including graphic design, photography, and digital art, to enhance the visual impact of images.

Python Pillow 库 (PIL) 在其 ImageEnhance 模块中提供了 Color() 类,它使您可以对图像应用颜色增强。

The Python Pillow library (PIL) offers the Color() class within its ImageEnhance module, which enables you to apply color enhancement to images.

Enhancing Color of an Image

要使用 Python Pillow 库增强图像颜色,您可以使用 ImageEnhance.Color() 类,它允许您调整图像的色彩平衡,就像通过彩色电视机上的控制一样。

To enhance the color of an image using the Python Pillow library, you can use the ImageEnhance.Color() class, which allows you to adjust the color balance of an image, just as controls on a color TV set.

以下是 ImageEnhance.Color() 类的语法:

Following is the syntax of the ImageEnhance.Color() class −

class PIL.ImageEnhance.Color(image)

将表示为浮点数的增强因子作为参数传递给常见的单接口方法 enhance(factor)。此因子在调整色彩平衡中起着重要作用。因子为 0.0 将生成一幅黑白图像。因子为 1.0 保留原图像。

An enhancement factor represented as a floating-point value, is passed as an argument to the common single interface method, enhance(factor). This factor plays an important role in adjusting the color balance. A factor of 0.0 will give a black and white image. A factor of 1.0 gives the original image.

以下步骤可实现图像的色彩增强:

Following are the steps to achieve color enhancement of an image −

  1. Create a color enhancer object using the ImageEnhance.Color() class.

  2. Then apply the enhancement factor to the enhancer object using the enhance() method.

Example

这里有一个示例,它使用因子 3.0 增强图像颜色。

Here is an example that enhances the color of an image with a factor of 3.0.

from PIL import Image, ImageEnhance

# Open an image file
image = Image.open('Images/Tajmahal_2.jpg')

# Create a Color object
color_enhancer = ImageEnhance.Color(image)

# Display the original image
image.show()

# Enhance the color with a factor of 3.0
colorful_image = color_enhancer.enhance(3.0)

# Display the enhanced color image
colorful_image.show()

输入图像 −

Input image −

tajmahal

输出颜色增强过的图像:

Output highly color enhanced image −

highly color enhanced

Example

这里有一个示例,它使用更低的增强因子 (0.0) 增强图像颜色。

Here is an example that enhances the color of an image with a lower enhancement factor(0.0).

from PIL import Image, ImageEnhance

# Open an image file
image = Image.open('Images/Tajmahal_2.jpg')

# Create a Color object
color_enhancer = ImageEnhance.Color(image)

# Display the original image
image.show()

# Enhance the color with a factor of 0.0
colorful_image = color_enhancer.enhance(0.0)

# Display the enhanced color image
colorful_image.show()

输入图像 −

Input image −

tajmahal

输出增强因子为 0.0 的颜色增强图像:

Output color enhanced image with a factor of 0.0 −

imageenhance zero