Spaces:
Sleeping
Sleeping
Upload image_helper.py
Browse files- src/modules/image_helper.py +18 -0
src/modules/image_helper.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
|
3 |
+
|
4 |
+
def crop_image_to_square(image_path):
|
5 |
+
image = Image.open(image_path)
|
6 |
+
width, height = image.size
|
7 |
+
size = min(width, height)
|
8 |
+
|
9 |
+
# Calculate the coordinates for the crop
|
10 |
+
left = (width - size) // 2
|
11 |
+
top = (height - size) // 2
|
12 |
+
right = left + size
|
13 |
+
bottom = top + size
|
14 |
+
|
15 |
+
cropped_image = image.crop((left, top, right, bottom))
|
16 |
+
|
17 |
+
# Override the original image with the cropped version
|
18 |
+
cropped_image.save(image_path)
|