分别使用 PIL.ImageEnhance
增强亮度、颜色和对比度功能时,图像如何转换?比如,为每个变换变换像素值的数学公式是什么?
最佳答案
来自source code on ImageEnhance.py
,我们看到任何转换都没有直接的“数学公式”。对于每种模态(亮度、颜色等),都会生成修改后的图像,然后根据 0.0 ... 1.0
中的给定因子将修改后的图像与原始图像混合,参见。 Image.blend
.
- 亮度:原始图像与相同大小的黑色图像混合。
- 颜色:原始图像与其灰度版本混合。
- 对比度:原始图像与相同大小的灰色图像混合。这是唯一会进行一些计算的点。灰度值由原始图像的灰度版本的平均值确定。
这是一些比较代码,从 ImageEnhancer
为一些 RGB 图像即时重建命名函数:
from matplotlib import pyplot as plt
from PIL import Image, ImageEnhance, ImageStat
# Read image, set up factor
image = Image.open('path/to/your/image.png')
factor = 0.25
# ImageEnhance
br_enhancer = ImageEnhance.Brightness(image)
cl_enhancer = ImageEnhance.Color(image)
cn_enhancer = ImageEnhance.Contrast(image)
# Rebuild ImageEnhance.Brightness on-the-fly
br_image_pre = Image.new(image.mode, image.size, 0)
br_image = Image.blend(br_image_pre, image, factor)
# Rebuild ImageEnhance.Color on-the-fly
cl_image_pre = image.convert('L').convert('RGB')
cl_image = Image.blend(cl_image_pre, image, factor)
# Rebuild ImageEnhance.Contrast on-the-fly
mean = int(ImageStat.Stat(image.convert('L')).mean[0] + 0.5)
cn_image_pre = Image.new('L', image.size, mean).convert(image.mode)
cn_image = Image.blend(cn_image_pre, image, factor)
# Visualization
plt.figure(1, figsize=(14, 9))
plt.subplot(3, 4, 1), plt.imshow(image), plt.title('Original image')
plt.subplot(3, 4, 2), plt.imshow(br_enhancer.enhance(factor)), plt.title('ImageEnhance.Brightness(0.25)')
plt.subplot(3, 4, 3), plt.imshow(cl_enhancer.enhance(factor)), plt.title('ImageEnhance.Color(0.25)')
plt.subplot(3, 4, 4), plt.imshow(cn_enhancer.enhance(factor)), plt.title('ImageEnhance.Contrast(0.25)')
plt.subplot(3, 4, 5), plt.imshow(image), plt.title('Original image (0.25)')
plt.subplot(3, 4, 6), plt.imshow(br_image_pre), plt.title('+ brightness modified image (0.75)')
plt.subplot(3, 4, 7), plt.imshow(cl_image_pre), plt.title('+ color modified image (0.75)')
plt.subplot(3, 4, 8), plt.imshow(cn_image_pre), plt.title('+ contrast modified image (0.75)')
plt.subplot(3, 4, 10), plt.imshow(br_image), plt.title('= rebuilt ImageEnhance.Brightness(0.25)')
plt.subplot(3, 4, 11), plt.imshow(cl_image), plt.title('= rebuilt ImageEnhance.Color(0.25)')
plt.subplot(3, 4, 12), plt.imshow(cn_image), plt.title('= rebuilt ImageEnhance.Contrast(0.25)')
plt.tight_layout()
plt.show()
这就是我的标准测试图像的输出:
希望对理解有所帮助!
关于python-3.x - PIL.ImageEnhance 增强颜色、亮度和对比度功能的公式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59166448/