88from PIL import Image , ImageFile , ImageSequence
99
1010
11- def transform_image (original_img : Image .Image , crop_w : int , crop_h : int ) -> list [Image .Image | ImageFile .ImageFile ]:
11+ def transform_image (original_img : Image .Image , crop_w : int , crop_h : int , center_point : tuple [ int , int ] = ( 0.5 , 0.5 ) ) -> list [Image .Image | ImageFile .ImageFile ]:
1212 """Resizes and crops the image to the specified crop_w and crop_h if necessary.
1313
1414 Works with multi frame gif and webp images.
@@ -22,6 +22,7 @@ def transform_image(original_img: Image.Image, crop_w: int, crop_h: int) -> list
2222 Instance of an Image or list of frames which they are instances of an Image individually
2323 """
2424 img_w , img_h = (original_img .size [0 ], original_img .size [1 ])
25+ # sequence?
2526 n_frames = getattr (original_img , "n_frames" , 1 )
2627
2728 def transform_frame (frame : Image .Image ) -> Image .Image | ImageFile .ImageFile :
@@ -52,9 +53,9 @@ def transform_frame(frame: Image.Image) -> Image.Image | ImageFile.ImageFile:
5253 new_w = crop_w if h_diff > w_diff else floor (crop_h * img_w / img_h )
5354 new_h = crop_h if h_diff < w_diff else floor (crop_w * img_h / img_w )
5455
55- left = (new_w - crop_w ) // 2
56+ left = (new_w - crop_w ) * center_point [ 0 ]
5657 right = left + crop_w
57- top = (new_h - crop_h ) // 2
58+ top = (new_h - crop_h ) * center_point [ 1 ]
5859 bottom = top + crop_h
5960
6061 return frame .resize ((new_w , new_h ), resample = Image .Resampling .LANCZOS ).crop ((left , top , right , bottom ))
0 commit comments