2019-07-21 11:03:39 +00:00
|
|
|
"""
|
|
|
|
Changing contrast with PIL
|
|
|
|
|
|
|
|
This algorithm is used in
|
2020-03-04 12:40:28 +00:00
|
|
|
https://noivce.pythonanywhere.com/ Python web app.
|
2019-07-21 11:03:39 +00:00
|
|
|
|
|
|
|
python/black: True
|
|
|
|
flake8 : True
|
|
|
|
"""
|
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
2020-10-03 07:22:22 +00:00
|
|
|
def change_contrast(img: Image, level: int) -> Image:
|
2019-07-21 11:03:39 +00:00
|
|
|
"""
|
|
|
|
Function to change contrast
|
|
|
|
"""
|
|
|
|
factor = (259 * (level + 255)) / (255 * (259 - level))
|
|
|
|
|
2020-10-03 07:22:22 +00:00
|
|
|
def contrast(c: int) -> int:
|
2019-07-21 11:03:39 +00:00
|
|
|
"""
|
|
|
|
Fundamental Transformation/Operation that'll be performed on
|
|
|
|
every bit.
|
|
|
|
"""
|
2020-10-03 07:22:22 +00:00
|
|
|
return int(128 + factor * (c - 128))
|
2019-07-21 11:03:39 +00:00
|
|
|
|
|
|
|
return img.point(contrast)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
# Load image
|
|
|
|
with Image.open("image_data/lena.jpg") as img:
|
|
|
|
# Change contrast to 170
|
|
|
|
cont_img = change_contrast(img, 170)
|
|
|
|
cont_img.save("image_data/lena_high_contrast.png", format="png")
|