From 6f80ca821c2adc02196962a6682104321ba82566 Mon Sep 17 00:00:00 2001 From: Furkan Atesli <31884209+furkanatesli@users.noreply.github.com> Date: Wed, 17 Jun 2020 08:49:20 +0300 Subject: [PATCH] Create change_brightness.py (#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 --- digital_image_processing/change_brightness.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 digital_image_processing/change_brightness.py diff --git a/digital_image_processing/change_brightness.py b/digital_image_processing/change_brightness.py new file mode 100644 index 000000000..97493f1a3 --- /dev/null +++ b/digital_image_processing/change_brightness.py @@ -0,0 +1,26 @@ +from PIL import Image + + +def change_brightness(img: Image, level: float) -> Image: + """ + Change the brightness of a PIL Image to a given level. + """ + + def brightness(c: int) -> float: + """ + Fundamental Transformation/Operation that'll be performed on + every bit. + """ + return 128 + level + (c - 128) + + if not -255.0 <= level <= 255.0: + raise ValueError("level must be between -255.0 (black) and 255.0 (white)") + return img.point(brightness) + + +if __name__ == "__main__": + # Load image + with Image.open("image_data/lena.jpg") as img: + # Change brightness to 100 + brigt_img = change_brightness(img, 100) + brigt_img.save("image_data/lena_brightness.png", format="png")