From 9b349a1f5837452928c6d3a74e9d58961c80d433 Mon Sep 17 00:00:00 2001 From: TheKekening Date: Sun, 10 Sep 2023 15:56:42 +0100 Subject: [PATCH 1/4] Modified to accept PIL.Image files and return PIL.Image files --- src/EE/pixelQR/ImageDecoding.py | 14 ++++++------ src/EE/pixelQR/ImageEncoding.py | 38 ++++++++++++++++----------------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/EE/pixelQR/ImageDecoding.py b/src/EE/pixelQR/ImageDecoding.py index b12ddf2..6e59a24 100644 --- a/src/EE/pixelQR/ImageDecoding.py +++ b/src/EE/pixelQR/ImageDecoding.py @@ -376,13 +376,13 @@ def resize_and_colour_correct(img): # return output_msg.decode("utf-8") -def photo_to_str(img_path): +def photo_to_str(im): """Converts photo to string Parameters ---------- - img_path : str - Input image path of Datastamp you wish to decode + img : Image + Input image of Datastamp you wish to decode (NOTE, code must have a white background). Returns @@ -391,11 +391,11 @@ def photo_to_str(img_path): Outputs data decoded from Datastamp. """ - with Image.open(img_path) as im: - imgsize = im.size - img = im.resize( - (math.floor(0.25*imgsize[0]), math.floor(0.25*imgsize[1])), 1) + imgsize = im.size + + img = im.resize( + (math.floor(0.25*imgsize[0]), math.floor(0.25*imgsize[1])), 1) img = centre_and_crop_img(img) img = resize_and_colour_correct(img) diff --git a/src/EE/pixelQR/ImageEncoding.py b/src/EE/pixelQR/ImageEncoding.py index 4354ca3..843e551 100644 --- a/src/EE/pixelQR/ImageEncoding.py +++ b/src/EE/pixelQR/ImageEncoding.py @@ -11,7 +11,7 @@ from PIL import Image, ImageOps -def str_to_image(secret_str: str, out_path: str, img_size: tuple = (32, 32)): +def str_to_image(secret_str: str, img_size: tuple = (32, 32)): """Converts input string to Datastamp Parameters @@ -30,21 +30,20 @@ def str_to_image(secret_str: str, out_path: str, img_size: tuple = (32, 32)): """ collist = str_to_colour_list2(secret_str) - colour_list_to_image(collist, img_size, out_path) - with Image.open(out_path) as im: - new_image = Image.new("RGB", (33, 34)) - new_image.paste(im, (1, 1)) - # The "new_image" variable is used to form the black C shape - # The black C shape is used for orientation purposes - w_background = Image.new("L", (35, 36)) - w_background = ImageOps.colorize( - w_background, (255, 255, 255), (255, 255, 255)) - w_background.paste(new_image, (1, 1)) - # The white background is necessary for the datastamp to successfully - # be scanned - new_image = w_background.resize((1000, 1000), 0) - new_image.save(out_path) - return out_path + im = colour_list_to_image(collist, img_size) + new_image = Image.new("RGB", (33, 34)) + new_image.paste(im, (1, 1)) + # The "new_image" variable is used to form the black C shape + # The black C shape is used for orientation purposes + w_background = Image.new("L", (35, 36)) + w_background = ImageOps.colorize( + w_background, (255, 255, 255), (255, 255, 255)) + w_background.paste(new_image, (1, 1)) + # The white background is necessary for the datastamp to successfully + # be scanned + new_image = w_background.resize((1000, 1000), 0) + # new_image.save(out_path) + return new_image def baseconvert2(num: int, base: int) -> str: @@ -134,7 +133,7 @@ def str_to_colour_list2(SecretMsg: str): return colour_list -def colour_list_to_image(colour_list: list, image_size: tuple, out_path: str): +def colour_list_to_image(colour_list: list, image_size: tuple): """Converts list of colours into the encoding portion of Datastamp Parameters @@ -201,12 +200,11 @@ def colour_list_to_image(colour_list: list, image_size: tuple, out_path: str): pixel_num = pixel_num + 1 background2.alpha_composite(background, (0, 0)) background2.alpha_composite(new_image, (0, 0)) - background2.save(out_path) - + return background2 if __name__ == "__main__": input_str = input( "Please input the message you would like to encode in image \n") output_path = input("Please input the output path of the encoded file \n") str_to_image(input_str, output_path) - print("done!") + print(output_path) From 03e35a57fe294a26d1058e5a6607d11bba512e67 Mon Sep 17 00:00:00 2001 From: TheKekening Date: Sun, 10 Sep 2023 15:58:09 +0100 Subject: [PATCH 2/4] Update ImageEncoding.py --- src/EE/pixelQR/ImageEncoding.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/EE/pixelQR/ImageEncoding.py b/src/EE/pixelQR/ImageEncoding.py index 843e551..1a62ca0 100644 --- a/src/EE/pixelQR/ImageEncoding.py +++ b/src/EE/pixelQR/ImageEncoding.py @@ -202,6 +202,7 @@ def colour_list_to_image(colour_list: list, image_size: tuple): background2.alpha_composite(new_image, (0, 0)) return background2 + if __name__ == "__main__": input_str = input( "Please input the message you would like to encode in image \n") From 827a80e1fd899f7f4f86814e3efddbb92ea46d78 Mon Sep 17 00:00:00 2001 From: TheKekening Date: Sun, 10 Sep 2023 16:01:13 +0100 Subject: [PATCH 3/4] Update ImageDecoding.py --- src/EE/pixelQR/ImageDecoding.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/EE/pixelQR/ImageDecoding.py b/src/EE/pixelQR/ImageDecoding.py index 6e59a24..67ca229 100644 --- a/src/EE/pixelQR/ImageDecoding.py +++ b/src/EE/pixelQR/ImageDecoding.py @@ -378,7 +378,6 @@ def resize_and_colour_correct(img): def photo_to_str(im): """Converts photo to string - Parameters ---------- img : Image @@ -391,7 +390,6 @@ def photo_to_str(im): Outputs data decoded from Datastamp. """ - imgsize = im.size img = im.resize( From 320bdfc4e0a48a5f7f4216e84e9db527682231d6 Mon Sep 17 00:00:00 2001 From: TheKekening Date: Sun, 10 Sep 2023 16:03:05 +0100 Subject: [PATCH 4/4] Update ImageDecoding.py --- src/EE/pixelQR/ImageDecoding.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/EE/pixelQR/ImageDecoding.py b/src/EE/pixelQR/ImageDecoding.py index 67ca229..123ebce 100644 --- a/src/EE/pixelQR/ImageDecoding.py +++ b/src/EE/pixelQR/ImageDecoding.py @@ -378,6 +378,7 @@ def resize_and_colour_correct(img): def photo_to_str(im): """Converts photo to string + Parameters ---------- img : Image