当前位置: 首页 > news >正文

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

 

相关文章:

  • Docker中部署Alertmanager
  • 从代码学习数学优化算法 - 拉格朗日松弛 Python版
  • 查看数据库占用磁盘空间的方法
  • JAVA面向对象——对象和类的基本语法
  • 第一章走进java世界
  • 数据库实验——备份与恢复
  • JavaScript 深拷贝:从基础到实践的全面指南
  • 2025年- H38-Lc146 --142.环形链表(快慢指针,快2慢1,快1慢1)--Java版
  • 前端流行框架Vue3教程:21. 插槽(3)
  • C语言| 指针变量的初始化
  • 如何测试北斗卫星通讯终端的性能?
  • DEBUG:Lombok 失效
  • C++类与对象--6 特性二:继承
  • std::vector<>.emplace_back
  • flutter设置最大高度,超过最大高度时滑动显示
  • 使用frp内网穿透本地的虚拟机
  • spring event事件(四)内部事件(1)ApplicationReadyEvent
  • 介绍Buildroot
  • 2025ICPC南昌邀请赛题解
  • 记录学习的第三十六天
  • 四川省政府党组成员、副省长、省公安厅厅长叶寒冰被查
  • 中国原创“地贫”基因编辑疗法新进展:复旦儿科医院治愈4名重型患儿
  • 媒体刊文:“假官号”层出不穷,平台要当好把关人
  • 欧盟宣布解除对叙利亚的经济制裁
  • 太平人寿党委书记赵峰调任海南省政府党组成员
  • 4月中国常青游戏榜:32款游戏吸金近34亿元,腾讯、网易占半壁江山,《原神》再跌出前十