Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 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()
|
|
@@ -35,25 +37,81 @@ def fill_square_cropper(img):
|
|
| 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(...)):
|
| 54 |
try:
|
| 55 |
-
# Await
|
| 56 |
-
contents = await file.read()
|
| 57 |
img = Image.open(BytesIO(contents)).convert("RGB")
|
| 58 |
squared_img = fill_square_cropper(img)
|
| 59 |
|
|
@@ -63,7 +121,6 @@ async def upload_file(file: UploadFile = File(...)): # Make the function asynch
|
|
| 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}' />",
|
|
@@ -72,7 +129,6 @@ async def upload_file(file: UploadFile = File(...)): # Make the function asynch
|
|
| 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)))
|
|
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile
|
| 2 |
from fastapi.responses import HTMLResponse
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
+
import requests
|
| 7 |
+
import base64
|
| 8 |
import os
|
| 9 |
|
| 10 |
app = FastAPI()
|
|
|
|
| 37 |
else:
|
| 38 |
return img
|
| 39 |
|
| 40 |
+
# Home Page
|
| 41 |
@app.get("/", response_class=HTMLResponse)
|
| 42 |
def home_page():
|
| 43 |
return """
|
| 44 |
<html>
|
| 45 |
<body>
|
| 46 |
<h2>Square and Fill Image App</h2>
|
| 47 |
+
<p>Select a tab below:</p>
|
| 48 |
+
<ul>
|
| 49 |
+
<li><a href="/demo">Demo</a></li>
|
| 50 |
+
<li><a href="/application">Application</a></li>
|
| 51 |
+
</ul>
|
| 52 |
+
</body>
|
| 53 |
+
</html>
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
# Demo Page
|
| 57 |
+
@app.get("/demo", response_class=HTMLResponse)
|
| 58 |
+
def demo_page():
|
| 59 |
+
# URLs for demo images
|
| 60 |
+
url1 = "https://raw.githubusercontent.com/webdevserv/images_video/main/cowportrait.jpg"
|
| 61 |
+
url2 = "https://raw.githubusercontent.com/webdevserv/images_video/main/cowlandscape.jpg"
|
| 62 |
+
|
| 63 |
+
# Process the first image
|
| 64 |
+
response = requests.get(url1)
|
| 65 |
+
img1 = Image.open(BytesIO(response.content)).convert("RGB")
|
| 66 |
+
squared_img1 = fill_square_cropper(img1)
|
| 67 |
+
output1 = BytesIO()
|
| 68 |
+
squared_img1.save(output1, format="JPEG")
|
| 69 |
+
encoded_img1 = base64.b64encode(output1.getvalue()).decode("utf-8")
|
| 70 |
+
|
| 71 |
+
# Process the second image
|
| 72 |
+
response = requests.get(url2)
|
| 73 |
+
img2 = Image.open(BytesIO(response.content)).convert("RGB")
|
| 74 |
+
squared_img2 = fill_square_cropper(img2)
|
| 75 |
+
output2 = BytesIO()
|
| 76 |
+
squared_img2.save(output2, format="JPEG")
|
| 77 |
+
encoded_img2 = base64.b64encode(output2.getvalue()).decode("utf-8")
|
| 78 |
+
|
| 79 |
+
return f"""
|
| 80 |
+
<html>
|
| 81 |
+
<body>
|
| 82 |
+
<h2>Square Image Demo</h2>
|
| 83 |
+
<p>Image will be squared with color filler where applicable.</p>
|
| 84 |
+
<h3>Result 1:</h3>
|
| 85 |
+
<img src="data:image/jpeg;base64,{encoded_img1}" />
|
| 86 |
+
<h3>Result 2:</h3>
|
| 87 |
+
<img src="data:image/jpeg;base64,{encoded_img2}" />
|
| 88 |
+
<a href="/">Back to Home</a>
|
| 89 |
+
</body>
|
| 90 |
+
</html>
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
+
# Application Page
|
| 94 |
+
@app.get("/application", response_class=HTMLResponse)
|
| 95 |
+
def application_page():
|
| 96 |
+
return """
|
| 97 |
+
<html>
|
| 98 |
+
<body>
|
| 99 |
+
<h2>Square Image Application</h2>
|
| 100 |
<p>Upload a JPG image to square and fill with color filler.</p>
|
| 101 |
<form action="/upload/" enctype="multipart/form-data" method="post">
|
| 102 |
<input name="file" type="file">
|
| 103 |
<input type="submit">
|
| 104 |
</form>
|
| 105 |
+
<a href="/">Back to Home</a>
|
| 106 |
</body>
|
| 107 |
</html>
|
| 108 |
"""
|
| 109 |
+
|
| 110 |
@app.post("/upload/")
|
| 111 |
+
async def upload_file(file: UploadFile = File(...)):
|
| 112 |
try:
|
| 113 |
+
# Await file upload
|
| 114 |
+
contents = await file.read()
|
| 115 |
img = Image.open(BytesIO(contents)).convert("RGB")
|
| 116 |
squared_img = fill_square_cropper(img)
|
| 117 |
|
|
|
|
| 121 |
output.seek(0)
|
| 122 |
|
| 123 |
# Return base64-encoded image
|
|
|
|
| 124 |
encoded_img = base64.b64encode(output.getvalue()).decode("utf-8")
|
| 125 |
return HTMLResponse(
|
| 126 |
content=f"<h3>Image successfully squared!</h3><img src='data:image/jpeg;base64,{encoded_img}' />",
|
|
|
|
| 129 |
except Exception as e:
|
| 130 |
return HTMLResponse(content=f"<h3>An error occurred: {e}</h3>", media_type="text/html")
|
| 131 |
|
|
|
|
| 132 |
if __name__ == "__main__":
|
| 133 |
import uvicorn
|
| 134 |
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|