from PIL import Image
def change_contrast(img: Image, level: int) -> Image:
factor = (259 * (level + 255)) / (255 * (259 - level))
def contrast(c: int) -> int:
return int(128 + factor * (c - 128))
return img.point(contrast)
if __name__ == "__main__":
# 加载图片
with Image.open("image_data/test.jpg") as img:
# 改变反差到170
cont_img = change_contrast(img, 170)
cont_img.save("image_data/test_high_contrast.png", format="png")
图片处理前:
图片处理后:
【欢迎关注编码小哥,学习更多实用的编程方法】?