分别使用PIL.ImageEnhance
增强功能分别设置亮度,颜色和对比度时如何转换图像?像这样,每次转换的像素值转换的数学公式是什么?
答案 0 :(得分:1)
从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()
然后,这是我的标准测试图像的输出:
希望有助于理解!