From 7590168238afc3301e237fae1ad7d8e0dfb9698a Mon Sep 17 00:00:00 2001 From: Rayan Dasoriya Date: Tue, 6 Aug 2024 23:07:14 +0530 Subject: [PATCH] feat: add resize image function to common util (#3399) Co-authored-by: Rayan Dasoriya --- .../model_oss/notebook_util/common_util.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/community-content/vertex_model_garden/model_oss/notebook_util/common_util.py b/community-content/vertex_model_garden/model_oss/notebook_util/common_util.py index 11864421a..aa54d252e 100644 --- a/community-content/vertex_model_garden/model_oss/notebook_util/common_util.py +++ b/community-content/vertex_model_garden/model_oss/notebook_util/common_util.py @@ -233,6 +233,22 @@ def download_image(url: str) -> str: return Image.open(io.BytesIO(response.content)) +def resize_image(image: Any, new_width: int = 1000) -> Any: + """Resizes an image to a certain width. + + Args: + image: The image which has to be resized. + new_width: New width of the image. + + Returns: + New resized image. + """ + width, height = image.size + new_height = int(height * new_width / width) + new_img = image.resize((new_width, new_height)) + return new_img + + def load_img(path: str) -> Any: """Reads image from path and return PIL.Image instance. @@ -433,3 +449,4 @@ def check_quota( f"Quota not enough for {resource_id} in {region}: {quota} <" f" {accelerator_count}. {quota_request_instruction}" ) +