PIL库的图像增强函数
1.前言
PIL库
图像颜色增强有四种方法
1.色度
2.亮度
3.对比度
4.锐度
2.代码
位于ImageEnhance.py
#
# The Python Imaging Library.
# $Id$
#
# image enhancement classes
#
# For a background, see "Image Processing By Interpolation and
# Extrapolation", Paul Haeberli and Douglas Voorhies. Available
# at http://www.graficaobscura.com/interp/index.html
#
# History:
# 1996-03-23 fl Created
# 2009-06-16 fl Fixed mean calculation
#
# Copyright (c) Secret Labs AB 1997.
# Copyright (c) Fredrik Lundh 1996.
#
# See the README file for information on usage and redistribution.
#from . import Image, ImageFilter, ImageStatclass _Enhance:def enhance(self, factor):"""Returns an enhanced image.:param factor: A floating point value controlling the enhancement.Factor 1.0 always returns a copy of the original image,lower factors mean less color (brightness, contrast,etc), and higher values more. There are no restrictionson this value.:rtype: :py:class:`~PIL.Image.Image`"""return Image.blend(self.degenerate, self.image, factor)class Color(_Enhance):"""Adjust image color balance.This class can be used to adjust the colour balance of an image, ina manner similar to the controls on a colour TV set. An enhancementfactor of 0.0 gives a black and white image. A factor of 1.0 givesthe original image."""def __init__(self, image):self.image = imageself.intermediate_mode = "L"if "A" in image.getbands():self.intermediate_mode = "LA"self.degenerate = image.convert(self.intermediate_mode).convert(image.mode)class Contrast(_Enhance):"""Adjust image contrast.This class can be used to control the contrast of an image, similarto the contrast control on a TV set. An enhancement factor of 0.0gives a solid grey image. A factor of 1.0 gives the original image."""def __init__(self, image):self.image = imagemean = int(ImageStat.Stat(image.convert("L")).mean[0] + 0.5)self.degenerate = Image.new("L", image.size, mean).convert(image.mode)if "A" in image.getbands():self.degenerate.putalpha(image.getchannel("A"))class Brightness(_Enhance):"""Adjust image brightness.This class can be used to control the brightness of an image. Anenhancement factor of 0.0 gives a black image. A factor of 1.0 gives theoriginal image."""def __init__(self, image):self.image = imageself.degenerate = Image.new(image.mode, image.size, 0)if "A" in image.getbands():self.degenerate.putalpha(image.getchannel("A"))class Sharpness(_Enhance):"""Adjust image sharpness.This class can be used to adjust the sharpness of an image. Anenhancement factor of 0.0 gives a blurred image, a factor of 1.0 gives theoriginal image, and a factor of 2.0 gives a sharpened image."""def __init__(self, image):self.image = imageself.degenerate = image.filter(ImageFilter.SMOOTH)if "A" in image.getbands():self.degenerate.putalpha(image.getchannel("A"))
函数说明:
这段代码实现了四种图像增强方法,每种方法都基于插值和外推技术。以下是它们的说明和区别:
### 1. **Color(色彩增强)**
- **功能**:调整图像的色彩饱和度。
- **原理**:通过将图像转换为灰度图(去色)作为退化图像,然后与原图进行插值或外推。
- **增强因子**:
- 0.0:完全去色(灰度图像)。
- 1.0:原始图像。
- >1.0:增加色彩饱和度。
- **适用场景**:调整图像的色彩鲜艳程度,例如在照片编辑中增强或减弱颜色。
### 2. **Contrast(对比度增强)**
- **功能**:调整图像的对比度。
- **原理**:以图像的平均亮度为基准生成退化图像(纯灰色图像),然后与原图进行插值或外推。
- **增强因子**:
- 0.0:纯灰色图像(无对比度)。
- 1.0:原始图像。
- >1.0:增加对比度。
- **适用场景**:调整图像的明暗差异,使图像更清晰或更柔和。
### 3. **Brightness(亮度增强)**
- **功能**:调整图像的亮度。
- **原理**:以全黑图像为退化图像,与原图进行插值或外推。
- **增强因子**:
- 0.0:全黑图像。
- 1.0:原始图像。
- >1.0:增加亮度。
- **适用场景**:调整图像的整体明暗程度,例如在低光环境下增亮图像。
### 4. **Sharpness(锐度增强)**
- **功能**:调整图像的锐度。
- **原理**:以模糊图像(通过平滑滤波得到)为退化图像,与原图进行插值或外推。
- **增强因子**:
- 0.0:模糊图像。
- 1.0:原始图像。
- >1.0:增加锐度(锐化图像)。
- **适用场景**:改善图像的清晰度,使边缘更明显。
这四种方法的主要区别在于它们使用的**退化图像**不同:色彩调整使用灰度图像,对比度调整使用平均亮度图像,亮度调整使用全黑图像,锐度调整使用模糊图像。
- **色彩增强**:适用于调整颜色鲜艳程度。
- **对比度增强**:适用于调整图像的明暗差异。
- **亮度增强**:适用于调整图像的整体明暗。
- **锐度增强**:适用于改善图像的清晰度和细节。
每种方法都可以通过调整增强因子来控制效果强度,支持从减弱到增强的连续变化。
参考资料:
1.kimi