Skip to content

Commit 98f5c59

Browse files
Add files via upload
1 parent c964d74 commit 98f5c59

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Changing contrast with PIL
3+
4+
This algorithm is used in
5+
https://noivce.pythonanywhere.com/ python web app.
6+
7+
python/black: True
8+
flake8 : True
9+
"""
10+
11+
from PIL import Image
12+
13+
14+
def change_contrast(img, level: int):
15+
"""
16+
Function to change contrast
17+
"""
18+
factor = (259 * (level + 255)) / (255 * (259 - level))
19+
20+
def contrast(c):
21+
"""
22+
Fundamental Transformation/Operation that'll be performed on
23+
every bit.
24+
"""
25+
return 128 + factor * (c - 128)
26+
27+
return img.point(contrast)
28+
29+
30+
if __name__ == "__main__":
31+
# Load image
32+
img = Image.open("image_data/lena.jpg")
33+
# Change contrast to 170
34+
cont_img = change_contrast(img, 170.0)
35+
cont_img.save("image_data/lena_high_contrast.png", format="png")

0 commit comments

Comments
 (0)