Skip to content

Commit 6f80ca8

Browse files
Create change_brightness.py (TheAlgorithms#2126)
* Create change_brightness.py * Update change_brightness.py * Update change_brightness.py * Update change_brightness.py * Update change_brightness.py Co-authored-by: Christian Clauss <[email protected]>
1 parent f97af65 commit 6f80ca8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from PIL import Image
2+
3+
4+
def change_brightness(img: Image, level: float) -> Image:
5+
"""
6+
Change the brightness of a PIL Image to a given level.
7+
"""
8+
9+
def brightness(c: int) -> float:
10+
"""
11+
Fundamental Transformation/Operation that'll be performed on
12+
every bit.
13+
"""
14+
return 128 + level + (c - 128)
15+
16+
if not -255.0 <= level <= 255.0:
17+
raise ValueError("level must be between -255.0 (black) and 255.0 (white)")
18+
return img.point(brightness)
19+
20+
21+
if __name__ == "__main__":
22+
# Load image
23+
with Image.open("image_data/lena.jpg") as img:
24+
# Change brightness to 100
25+
brigt_img = change_brightness(img, 100)
26+
brigt_img.save("image_data/lena_brightness.png", format="png")

0 commit comments

Comments
 (0)