当前位置: 首页 > 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

 

http://www.xdnf.cn/news/543187.html

相关文章:

  • 从ISO17025合规到信创适配 解密质检lims系统实验室的 AI 质检全链路实践
  • 【C++】C++的拷贝构造函数介绍使用
  • 【RK3588嵌入式图形编程】-Cairo-形状与填充
  • C++从入门到实战(十六)String(中)String的常用接口(构造接口,析构接口,迭代器,遍历修改,容量管理与数据访问)
  • 零基础设计模式——创建型模式 - 单例模式
  • .NET 10 - 尝试一下Minimal Api的Validation新特性
  • 开源一个记账软件,支持docker一键部署
  • APPtrace 智能参数系统:重构 App 用户增长与运营逻辑
  • C++中String类
  • 《经济日报》深度聚焦|珈和科技携手万果博览荟共筑智慧农业新示范高地 全链赋能蒲江茶果产业数字化转型升级
  • 榕壹云上门家政系统:基于Spring Boot+MySQL+UniApp的全能解决方案
  • 深度剖析ZooKeeper
  • 基于大模型与人工智能体的机械臂对话式交互系统RobotAgent
  • 阿里云CDN刷新预热--刷新URL
  • 【AI 大模型】盘古大模型简介 ( 创建空间 | 体验模型 | 部署模型 )
  • 【华为OD-B卷-打印文件 100分(python、java、c++、js、c)】
  • 面试算法刷题3(核心+acm)
  • LVS原理详解及LVS负载均衡工作模式
  • Java的线程池相关的几个问题
  • Python 训练营打卡 Day 20-奇异值SVD分解
  • STM32F103_LL库+寄存器学习笔记12.2 - 串口DMA高效收发实战2:进一步提高串口接收的效率
  • Java实现基于bitmap的字符串去重统计
  • 鸿蒙路由参数传递
  • sqlite
  • Django快速入门篇
  • 基于 ESP32 与 AWS 全托管服务的 IoT 架构:MQTT + WebSocket 实现设备-云-APP 高效互联
  • 2025年渗透测试面试题总结-华顺信安[实习]安全服务工程师(题目+回答)
  • sqlite的拼接字段的方法(sqlite没有convert函数)
  • STL中list的模拟
  • React 第四十三节 Router中 useBlocker 的使用详解及案例注意事项