@@ -35,34 +35,36 @@ def smart_resize(
3535 height : int , width : int , factor : int = IMAGE_FACTOR , min_pixels : int = MIN_PIXELS , max_pixels : int = MAX_PIXELS
3636) -> tuple [int , int ]:
3737
38- if max (height , width ) / min (height , width ) > MAX_RATIO :
38+ if max (height , width ) / min (height , width ) > 200 :
3939 raise ValueError (
40- f"absolute aspect ratio must be smaller than { MAX_RATIO } , got { max (height , width ) / min (height , width )} "
40+ f"absolute aspect ratio must be smaller than 200 , got { max (height , width ) / min (height , width )} "
4141 )
42- h_bar = max ( factor , round (height / factor ) * factor )
43- w_bar = max ( factor , round (width / factor ) * factor )
42+ h_bar = round (height / factor ) * factor
43+ w_bar = round (width / factor ) * factor
4444 if h_bar * w_bar > max_pixels :
4545 beta = math .sqrt ((height * width ) / max_pixels )
46- h_bar = math .floor (height / beta / factor ) * factor
47- w_bar = math .floor (width / beta / factor ) * factor
46+ h_bar = max ( factor , math .floor (height / beta / factor ) * factor )
47+ w_bar = max ( factor , math .floor (width / beta / factor ) * factor )
4848 elif h_bar * w_bar < min_pixels :
4949 beta = math .sqrt (min_pixels / (height * width ))
5050 h_bar = math .ceil (height * beta / factor ) * factor
5151 w_bar = math .ceil (width * beta / factor ) * factor
5252 return h_bar , w_bar
5353
5454
55- def resize_image (image_file : Image .Image , size_factor : int = IMAGE_FACTOR ) -> tuple [Image .Image , int , int ]:
55+ def resize_image (
56+ image_file : Image .Image , factor : int = IMAGE_FACTOR , min_pixels : int = MIN_PIXELS , max_pixels : int = MAX_PIXELS
57+ ) -> tuple [Image .Image , int , int ]:
5658
5759 image = image_file .convert ("RGB" )
5860 width , height = image .size
5961
6062 resized_height , resized_width = smart_resize (
6163 height ,
6264 width ,
63- factor = size_factor ,
64- min_pixels = MIN_PIXELS ,
65- max_pixels = MAX_PIXELS ,
65+ factor = factor ,
66+ min_pixels = min_pixels ,
67+ max_pixels = max_pixels ,
6668 )
6769 image = image .resize ((resized_width , resized_height ))
6870
0 commit comments