From bed7e101e0bb60d26024b89af97300046c2959d6 Mon Sep 17 00:00:00 2001 From: Furkan Atesli <31884209+furkanatesli@users.noreply.github.com> Date: Tue, 16 Jun 2020 22:58:25 +0300 Subject: [PATCH 1/5] Create change_brightness.py --- digital_image_processing/change_brightness.py | 23 +++++++++++++++++++ 1 file changed, 23 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 000000000000..5a98d81061f5 --- /dev/null +++ b/digital_image_processing/change_brightness.py @@ -0,0 +1,23 @@ +from PIL import Image + +def change_brightness(img: Image, level: float) -> Image: + """ + Function to change brightness + """ + factor = (259 + (level + 255)) / (255 + (259 - level)) + + def brightness(c: int) -> float: + """ + Fundamental Transformation/Operation that'll be performed on + every bit. + """ + return 128 + factor * (c - 128) + + return img.point(brightness) + +if __name__ == "__main__": + # Load image + with Image.open("image_data/lena.jpg") as img: + # Change brightness to 170 + cont_img = change_brightness(img, 300) + cont_img.save("image_data/lena_high_contrast.png", format="png") From ecd8228770895081e3bd19bd74d01e6f370ba44c Mon Sep 17 00:00:00 2001 From: Furkan Atesli <31884209+furkanatesli@users.noreply.github.com> Date: Tue, 16 Jun 2020 23:31:12 +0300 Subject: [PATCH 2/5] Update change_brightness.py --- digital_image_processing/change_brightness.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/digital_image_processing/change_brightness.py b/digital_image_processing/change_brightness.py index 5a98d81061f5..d664832e9f06 100644 --- a/digital_image_processing/change_brightness.py +++ b/digital_image_processing/change_brightness.py @@ -2,7 +2,7 @@ def change_brightness(img: Image, level: float) -> Image: """ - Function to change brightness + Change the brightness of a PIL Image to a given level. """ factor = (259 + (level + 255)) / (255 + (259 - level)) @@ -19,5 +19,5 @@ def brightness(c: int) -> float: # Load image with Image.open("image_data/lena.jpg") as img: # Change brightness to 170 - cont_img = change_brightness(img, 300) - cont_img.save("image_data/lena_high_contrast.png", format="png") + brigt_img = change_brightness(img, 170) + brigt_img.save("image_data/lena_high_contrast.png", format="png") From 3fae49d6549390436c7641ad9bb3a9af6e1a5a5a Mon Sep 17 00:00:00 2001 From: Furkan Atesli <31884209+furkanatesli@users.noreply.github.com> Date: Wed, 17 Jun 2020 01:18:09 +0300 Subject: [PATCH 3/5] Update change_brightness.py --- digital_image_processing/change_brightness.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/digital_image_processing/change_brightness.py b/digital_image_processing/change_brightness.py index d664832e9f06..2531a93af4ef 100644 --- a/digital_image_processing/change_brightness.py +++ b/digital_image_processing/change_brightness.py @@ -1,23 +1,24 @@ from PIL import Image + def change_brightness(img: Image, level: float) -> Image: """ Change the brightness of a PIL Image to a given level. """ - factor = (259 + (level + 255)) / (255 + (259 - level)) - + def brightness(c: int) -> float: """ Fundamental Transformation/Operation that'll be performed on every bit. """ - return 128 + factor * (c - 128) + return 128 + level + (c - 128) return img.point(brightness) + if __name__ == "__main__": # Load image with Image.open("image_data/lena.jpg") as img: - # Change brightness to 170 - brigt_img = change_brightness(img, 170) - brigt_img.save("image_data/lena_high_contrast.png", format="png") + # Change brightness to 100 + brigt_img = change_brightness(img, 100) + brigt_img.save("image_data/lena_brightness.png", format="png") From c9b99781e7bff4b38d2b83f75834d52493bb0a3a Mon Sep 17 00:00:00 2001 From: Furkan Atesli <31884209+furkanatesli@users.noreply.github.com> Date: Wed, 17 Jun 2020 01:23:22 +0300 Subject: [PATCH 4/5] Update change_brightness.py --- digital_image_processing/change_brightness.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/digital_image_processing/change_brightness.py b/digital_image_processing/change_brightness.py index 2531a93af4ef..c0682dea3c70 100644 --- a/digital_image_processing/change_brightness.py +++ b/digital_image_processing/change_brightness.py @@ -5,7 +5,7 @@ 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 From d94f7016d77131f13650e2a502b9859d10933942 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 17 Jun 2020 07:14:16 +0200 Subject: [PATCH 5/5] Update change_brightness.py --- digital_image_processing/change_brightness.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/digital_image_processing/change_brightness.py b/digital_image_processing/change_brightness.py index c0682dea3c70..97493f1a399e 100644 --- a/digital_image_processing/change_brightness.py +++ b/digital_image_processing/change_brightness.py @@ -13,6 +13,8 @@ def brightness(c: int) -> float: """ 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)