Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ app = FastAPI()
|
|
| 14 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 15 |
|
| 16 |
# Function for cropping and filling the image
|
| 17 |
-
def
|
| 18 |
imgsz = [img.height, img.width]
|
| 19 |
avg_color_per_row = np.average(img, axis=0)
|
| 20 |
avg_color = np.average(avg_color_per_row, axis=0)
|
|
@@ -25,7 +25,8 @@ def fill_square_cropper(img):
|
|
| 25 |
(img.height, img.height),
|
| 26 |
(round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))
|
| 27 |
)
|
| 28 |
-
newpos = (img.
|
|
|
|
| 29 |
newimg.paste(img, (newpos, 0))
|
| 30 |
return newimg
|
| 31 |
|
|
@@ -35,7 +36,7 @@ def fill_square_cropper(img):
|
|
| 35 |
(img.width, img.width),
|
| 36 |
(round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))
|
| 37 |
)
|
| 38 |
-
newpos = (img.
|
| 39 |
newimg.paste(img, (0, newpos))
|
| 40 |
return newimg
|
| 41 |
else:
|
|
@@ -109,7 +110,7 @@ def demo_page():
|
|
| 109 |
# Process the first image
|
| 110 |
response = requests.get(url1)
|
| 111 |
img1 = Image.open(BytesIO(response.content)).convert("RGB")
|
| 112 |
-
squared_img1 =
|
| 113 |
output1 = BytesIO()
|
| 114 |
squared_img1.save(output1, format="JPEG")
|
| 115 |
encoded_img1 = base64.b64encode(output1.getvalue()).decode("utf-8")
|
|
@@ -117,7 +118,7 @@ def demo_page():
|
|
| 117 |
# Process the second image
|
| 118 |
response = requests.get(url2)
|
| 119 |
img2 = Image.open(BytesIO(response.content)).convert("RGB")
|
| 120 |
-
squared_img2 =
|
| 121 |
output2 = BytesIO()
|
| 122 |
squared_img2.save(output2, format="JPEG")
|
| 123 |
encoded_img2 = base64.b64encode(output2.getvalue()).decode("utf-8")
|
|
@@ -169,7 +170,7 @@ async def upload_file(file: UploadFile = File(...)):
|
|
| 169 |
# Await file upload
|
| 170 |
contents = await file.read()
|
| 171 |
img = Image.open(BytesIO(contents)).convert("RGB")
|
| 172 |
-
squared_img =
|
| 173 |
|
| 174 |
# Save the squared image (original size)
|
| 175 |
output = BytesIO()
|
|
|
|
| 14 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 15 |
|
| 16 |
# Function for cropping and filling the image
|
| 17 |
+
def fill_rectangle_cropper(img):
|
| 18 |
imgsz = [img.height, img.width]
|
| 19 |
avg_color_per_row = np.average(img, axis=0)
|
| 20 |
avg_color = np.average(avg_color_per_row, axis=0)
|
|
|
|
| 25 |
(img.height, img.height),
|
| 26 |
(round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))
|
| 27 |
)
|
| 28 |
+
newpos = (img.width - img.height) // 2
|
| 29 |
+
|
| 30 |
newimg.paste(img, (newpos, 0))
|
| 31 |
return newimg
|
| 32 |
|
|
|
|
| 36 |
(img.width, img.width),
|
| 37 |
(round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))
|
| 38 |
)
|
| 39 |
+
newpos = (img.height - img.width) // 2
|
| 40 |
newimg.paste(img, (0, newpos))
|
| 41 |
return newimg
|
| 42 |
else:
|
|
|
|
| 110 |
# Process the first image
|
| 111 |
response = requests.get(url1)
|
| 112 |
img1 = Image.open(BytesIO(response.content)).convert("RGB")
|
| 113 |
+
squared_img1 = fill_rectangle_cropper_cropper(img1)
|
| 114 |
output1 = BytesIO()
|
| 115 |
squared_img1.save(output1, format="JPEG")
|
| 116 |
encoded_img1 = base64.b64encode(output1.getvalue()).decode("utf-8")
|
|
|
|
| 118 |
# Process the second image
|
| 119 |
response = requests.get(url2)
|
| 120 |
img2 = Image.open(BytesIO(response.content)).convert("RGB")
|
| 121 |
+
squared_img2 = fill_rectangle_cropper_cropper(img2)
|
| 122 |
output2 = BytesIO()
|
| 123 |
squared_img2.save(output2, format="JPEG")
|
| 124 |
encoded_img2 = base64.b64encode(output2.getvalue()).decode("utf-8")
|
|
|
|
| 170 |
# Await file upload
|
| 171 |
contents = await file.read()
|
| 172 |
img = Image.open(BytesIO(contents)).convert("RGB")
|
| 173 |
+
squared_img = fill_rectangle_cropper_cropper(img)
|
| 174 |
|
| 175 |
# Save the squared image (original size)
|
| 176 |
output = BytesIO()
|