feat: add resize image function to common util (#3399)

Co-authored-by: Rayan Dasoriya <dasoriya@google.com>
This commit is contained in:
Rayan Dasoriya
2024-08-06 17:37:14 +00:00
committed by GitHub
co-authored by Rayan Dasoriya
parent 5cac0e6d0b
commit 7590168238
@@ -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}"
)