Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions digital_image_processing/change_contrast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Changing contrast with PIL

This algorithm is used in
https://noivce.pythonanywhere.com/ python web app.
Comment thread
cclauss marked this conversation as resolved.

python/black: True
flake8 : True
"""

from PIL import Image


def change_contrast(img: Image, level: float) -> Image:
"""
Function to change contrast
"""
factor = (259 * (level + 255)) / (255 * (259 - level))

def contrast(c: int) -> float:
Comment thread
cclauss marked this conversation as resolved.
"""
Fundamental Transformation/Operation that'll be performed on
every bit.
"""
return 128 + factor * (c - 128)

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")
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mypy
numpy
opencv-python
pandas
pillow
pytest
sklearn
sympy
Expand Down