Skip to content

Commit 532e945

Browse files
committed
add img helpers
1 parent 3d52d25 commit 532e945

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

ipyplot/img_helpers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def resize_with_aspect_ratio(img, max_size=1000):
2+
"""Helper function to resize image against the longer edge
3+
4+
Args:
5+
img (PIL.Image):
6+
Image object to be resized
7+
max_size (int, optional):
8+
Max size of the longer edge in pixels.
9+
Defaults to 2800.
10+
11+
Returns:
12+
PIL.Image:
13+
Resized image object
14+
"""
15+
w, h = img.size
16+
aspect_ratio = min(max_size/w, max_size/h)
17+
resized_img = img.resize(
18+
(int(w * aspect_ratio), int(h * aspect_ratio))
19+
)
20+
return resized_img

0 commit comments

Comments
 (0)