Spaces:
Running
Running
Delete appworking.py
Browse files- appworking.py +0 -78
appworking.py
DELETED
@@ -1,78 +0,0 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile
|
2 |
-
from fastapi.responses import HTMLResponse
|
3 |
-
from PIL import Image
|
4 |
-
import numpy as np
|
5 |
-
from io import BytesIO
|
6 |
-
import os
|
7 |
-
|
8 |
-
app = FastAPI()
|
9 |
-
|
10 |
-
# Function for cropping and filling the image
|
11 |
-
def fill_square_cropper(img):
|
12 |
-
imgsz = [img.height, img.width]
|
13 |
-
avg_color_per_row = np.average(img, axis=0)
|
14 |
-
avg_color = np.average(avg_color_per_row, axis=0)
|
15 |
-
|
16 |
-
if img.height > img.width:
|
17 |
-
newimg = Image.new(
|
18 |
-
'RGB',
|
19 |
-
(img.height, img.height),
|
20 |
-
(round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))
|
21 |
-
)
|
22 |
-
newpos = (img.height - img.width) // 2
|
23 |
-
newimg.paste(img, (newpos, 0))
|
24 |
-
return newimg
|
25 |
-
|
26 |
-
elif img.width > img.height:
|
27 |
-
newimg = Image.new(
|
28 |
-
'RGB',
|
29 |
-
(img.width, img.width),
|
30 |
-
(round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))
|
31 |
-
)
|
32 |
-
newpos = (img.width - img.height) // 2
|
33 |
-
newimg.paste(img, (0, newpos))
|
34 |
-
return newimg
|
35 |
-
else:
|
36 |
-
return img
|
37 |
-
|
38 |
-
@app.get("/", response_class=HTMLResponse)
|
39 |
-
def home_page():
|
40 |
-
return """
|
41 |
-
<html>
|
42 |
-
<body>
|
43 |
-
<h2>Square and Fill Image App</h2>
|
44 |
-
<p>Upload a JPG image to square and fill with color filler.</p>
|
45 |
-
<form action="/upload/" enctype="multipart/form-data" method="post">
|
46 |
-
<input name="file" type="file">
|
47 |
-
<input type="submit">
|
48 |
-
</form>
|
49 |
-
</body>
|
50 |
-
</html>
|
51 |
-
"""
|
52 |
-
@app.post("/upload/")
|
53 |
-
async def upload_file(file: UploadFile = File(...)): # Make the function asynchronous
|
54 |
-
try:
|
55 |
-
# Await the read method
|
56 |
-
contents = await file.read()
|
57 |
-
img = Image.open(BytesIO(contents)).convert("RGB")
|
58 |
-
squared_img = fill_square_cropper(img)
|
59 |
-
|
60 |
-
# Save the squared image
|
61 |
-
output = BytesIO()
|
62 |
-
squared_img.save(output, format="JPEG")
|
63 |
-
output.seek(0)
|
64 |
-
|
65 |
-
# Return base64-encoded image
|
66 |
-
import base64
|
67 |
-
encoded_img = base64.b64encode(output.getvalue()).decode("utf-8")
|
68 |
-
return HTMLResponse(
|
69 |
-
content=f"<h3>Image successfully squared!</h3><img src='data:image/jpeg;base64,{encoded_img}' />",
|
70 |
-
media_type="text/html"
|
71 |
-
)
|
72 |
-
except Exception as e:
|
73 |
-
return HTMLResponse(content=f"<h3>An error occurred: {e}</h3>", media_type="text/html")
|
74 |
-
|
75 |
-
|
76 |
-
if __name__ == "__main__":
|
77 |
-
import uvicorn
|
78 |
-
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|