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")
图片亮度处理前:
图片亮度处理后:
【欢迎关注编码小哥,学习更多实用的编程方法】