Skip to content
Closed
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 fixing PIL issues in pytest output
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#author-kingslayer8509@gmail.com
"""
Changing contrast with PIL
This algorithm is used in
https://noivce.pythonanywhere.com/ Python web app.
python/black: True
flake8 : True
"""
from PIL import Image


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

def contrast(c: int) -> float:
def contrast(c: int) -> int:
"""
Fundamental Transformation/Operation that'll be performed on
every bit.
"""
return 128 + factor * (c - 128)
return int(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")