Skip to content

Commit 05e567c

Browse files
QuantumNovicecclauss
authored andcommitted
Code to change contrast (#1060)
* Add files via upload * Update requirements.txt * Add files via upload * Add files via upload * Add files via upload * Add files via upload
1 parent c964d74 commit 05e567c

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-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: Image, level: float) -> Image:
15+
"""
16+
Function to change contrast
17+
"""
18+
factor = (259 * (level + 255)) / (255 * (259 - level))
19+
20+
def contrast(c: int) -> float:
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+
with Image.open("image_data/lena.jpg") as img:
33+
# Change contrast to 170
34+
cont_img = change_contrast(img, 170)
35+
cont_img.save("image_data/lena_high_contrast.png", format="png")

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mypy
55
numpy
66
opencv-python
77
pandas
8+
pillow
89
pytest
910
sklearn
1011
sympy

0 commit comments

Comments
 (0)