图像处理------亮度

python 复制代码
from PIL import Image

def change_brightness(img: Image, level: float) -> Image:
    """
    按照给定的亮度等级,改变图片的亮度
    """

    def brightness(c: int) -> float:
        return 128 + level + (c - 128)

    if not -255.0 <= level <= 255.0:
        raise ValueError("level must be between -255.0 (black) and 255.0 (white)")
    return img.point(brightness)


if __name__ == "__main__":
    # 加载图片
    with Image.open("image_data/test.jpg") as img:
        # 改变亮度到100
        brigt_img = change_brightness(img, 100)
        brigt_img.save("image_data/test_brightness.png", format="png")

图片亮度处理前:

图片亮度处理后:

【欢迎关注编码小哥,学习更多实用的编程方法】

相关推荐
李昊哲小课21 小时前
Python办公自动化教程 - 第7章 综合实战案例 - 企业销售管理系统
开发语言·python·数据分析·excel·数据可视化·openpyxl
不知名的老吴21 小时前
返回None还是空集合?防御式编程的关键细节
开发语言·python
李昊哲小课1 天前
Python办公自动化教程 - 第5章 图表创建 - 让数据可视化
python·信息可视化·数据分析·数据可视化·openpyxl
chushiyunen1 天前
python pygame实现贪食蛇
开发语言·python·pygame
Dream of maid1 天前
Python-基础2(流程控制)
python
Lenyiin1 天前
《Python 修炼全景指南:一》从环境搭建到第一个程序
开发语言·python
涛声依旧393161 天前
Python项目实战:学生信息管理系统
开发语言·python·数据挖掘
kcuwu.1 天前
Python进阶:生成器与协程,高效并发编程的核心实践
windows·python·php
XiaoQiao6669991 天前
python 简单题目练手【详解版】【1】
开发语言·python
ZC跨境爬虫1 天前
极验滑动验证码自动化实战:背景提取、缺口定位与Playwright滑动模拟
前端·爬虫·python·自动化