python-3.x - PIL.ImageEnhance 增强颜色、亮度和对比度功能的公式是什么?

标签 python-3.x image image-processing python-imaging-library

分别使用 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()

这就是我的标准测试图像的输出:

Output

希望对理解有所帮助!

关于python-3.x - PIL.ImageEnhance 增强颜色、亮度和对比度功能的公式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59166448/

相关文章:

jquery - 如果我的范围 slider = 0.5,如何更改按钮图像? (js)

python - 使用python,如何计算图像中具有指定尺寸的对象的面积

python-3.x - 如果 Feather 文件格式仍然相关,或者社区是否倾向于使用其他文件格式来存储大文件?

php 将图像与其在不同位置的翻转和旋转 View 合并

html - 任何预览高清照片的技术,可以像谷歌地图一样放大和缩小

matlab - 代表星图的两个向量之间的相似性

python - 为什么提取出来的水印和嵌入的水印不一样?

python - OpenCV 大图像(大于 1gb)替代方案

python - 如何返回多字典的键值?

python - 清理 pandas Dataframe 中的单列