changing contrast with pil this algorithm is used in https noivce pythonanywhere com python web app psfblack true ruff true function to change contrast fundamental transformationoperation that ll be performed on every bit load image change contrast to 170 function to change contrast fundamental transformation operation that ll be performed on every bit load image change contrast to 170
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/lena.jpg") as img:
        
        cont_img = change_contrast(img, 170)
        cont_img.save("image_data/lena_high_contrast.png", format="png")