From 6d253a0e988ef99d0d1bc545dc4c105c71d7062d Mon Sep 17 00:00:00 2001 From: AndersLan <andeslan@stud.ntnu.no> Date: Wed, 9 Jun 2021 14:35:13 +0200 Subject: [PATCH] removed percentage scaling --- api/endpoints/salamander.py | 43 +++++++++--------------- api/endpoints/salamanderimageid.py | 53 ++++++++++-------------------- 2 files changed, 34 insertions(+), 62 deletions(-) diff --git a/api/endpoints/salamander.py b/api/endpoints/salamander.py index 2cf5930..576bc43 100644 --- a/api/endpoints/salamander.py +++ b/api/endpoints/salamander.py @@ -34,38 +34,27 @@ class SalamanderEndpoint(Resource): image = cv2.imread(path) - # percent of original size - scale_percent = 40 - width = int(image.shape[1] * scale_percent / 100) - height = int(image.shape[0] * scale_percent / 100) - dim = (width, height) + #scaling to set size + height, width, _ = image.shape - # resize image - resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA) + desired_long_side = 320 + scaling_factor = 1 - encoded = base64.b64encode(cv2.imencode(".jpg", resized)[1]).decode() + if width > height: + scaling_factor = desired_long_side / width + else: + scaling_factor = desired_long_side / height - #scaling to set size - # height, width, _ = image.shape - # - # desired_long_side = 150 - # scaling_factor = 1 - # - # if width > height: - # scaling_factor = desired_long_side / width - # else: - # scaling_factor = desired_long_side / height - # - # new_dim = (int(width * scaling_factor), int(height * scaling_factor)) - # - # # resize image - # image = cv2.resize(image, new_dim, interpolation=cv2.INTER_AREA) - # - # _, buffer = cv2.imencode('.jpg', image) - # encoded = base64.b64encode(buffer) + new_dim = (int(width * scaling_factor), int(height * scaling_factor)) + + # resize image + image = cv2.resize(image, new_dim, interpolation=cv2.INTER_AREA) + + _, buffer = cv2.imencode('.jpg', image) + encoded = base64.b64encode(buffer) image_id = os.path.basename(path)[0] - image_data = {"id": image_id, "url": "data:image/png;base64," + encoded} #Decode her om bestemt størrelse skal brukes + image_data = {"id": image_id, "url": "data:image/png;base64," + encoded.decode()} salamander_images.append(image_data) salamander_images.sort(key=lambda x: x.get('id')) diff --git a/api/endpoints/salamanderimageid.py b/api/endpoints/salamanderimageid.py index 97894fd..6898d53 100644 --- a/api/endpoints/salamanderimageid.py +++ b/api/endpoints/salamanderimageid.py @@ -39,46 +39,29 @@ class SalamanderImageEndpoint(Resource): if basename.__contains__(str(image_id)): image = cv2.imread(path) + if not basename.__contains__("str"): #Scale only original to set size - # percent of original size - scale_percent = 20 - width = int(image.shape[1] * scale_percent / 100) - height = int(image.shape[0] * scale_percent / 100) - dim = (width, height) + height, width, _ = image.shape + print(sys.getsizeof(image)) - # resize image - resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA) + desired_long_side = 320 + scaling_factor = 1 - encoded = base64.b64encode(cv2.imencode(".jpg", resized)[1]).decode() + if width > height: + scaling_factor = desired_long_side/width + else: + scaling_factor = desired_long_side/height - image_data = {"url": "data:image/png;base64," + encoded} + new_dim = (int(width*scaling_factor), int(height*scaling_factor)) - #Scale only original to set size - # image = cv2.imread(path) - # if not basename.__contains__("str"): - # - # height, width, _ = image.shape - # print(sys.getsizeof(image)) - # - # desired_long_side = 320 - # scaling_factor = 1 - # - # if width > height: - # scaling_factor = desired_long_side/width - # else: - # scaling_factor = desired_long_side/height - # - # new_dim = (int(width*scaling_factor), int(height*scaling_factor)) - # - # # resize image - # image = cv2.resize(image, new_dim, interpolation=cv2.INTER_AREA) - # print(sys.getsizeof(image)) - # - # _, buffer = cv2.imencode('.jpg', image) - # encoded = base64.b64encode(buffer) - # - # #encoded = encode(path)[2:-1] - # image_data = {"url": "data:image/png;base64," + encoded.decode()} + # resize image + image = cv2.resize(image, new_dim, interpolation=cv2.INTER_AREA) + print(sys.getsizeof(image)) + + _, buffer = cv2.imencode('.jpg', image) + encoded = base64.b64encode(buffer) + + image_data = {"url": "data:image/png;base64," + encoded.decode()} salamander_images.append(image_data) -- GitLab