-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
80 lines (60 loc) · 2.7 KB
/
server.py
File metadata and controls
80 lines (60 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Small server to diplay local images in a grid
"""
from fastapi import FastAPI, Depends, HTTPException
from fastapi.templating import Jinja2Templates
from starlette.requests import Request
from fastapi.responses import StreamingResponse
import os
import io
from PIL import Image
import re
app = FastAPI()
templates = Jinja2Templates(directory="templates")
def create_thumbnails(image_files, directory):
print("creating thumbnails")
base_height = 600
os.makedirs(os.path.join(directory, "thumbnails"), exist_ok=True)
for img_file in image_files:
# Open an image with PIL
img = Image.open(os.path.join(directory, img_file))
# Resize while maintaining the aspect ratio
hpercent = base_height / float(img.size[1])
wsize = int(float(img.size[0]) * float(hpercent))
img = img.resize((wsize, base_height), Image.LANCZOS)
# Save image to buffer
img.save(os.path.join(directory, "thumbnails", img_file), format='JPEG', quality=85)
return os.path.join(directory, "thumbnails")
def extract_numbers(filename):
# Extract x and y values from the filename
match = re.search(r'Epoch_(\d+)_(\d+).png', filename)
if match:
return (int(match.group(1)), int(match.group(2)))
return (0, 0)
@app.get("/")
async def read_root(request: Request, images_dir: str = "images"):
print(images_dir)
print(os.getcwd())
if not os.path.exists(images_dir):
raise HTTPException(status_code=404, detail="Directory not found")
# List all files in the image directory
image_files = sorted([f for f in os.listdir(images_dir) if os.path.isfile(os.path.join(images_dir, f))],
key=extract_numbers)
print(image_files)
thumbnail_dir_name = create_thumbnails(image_files, images_dir)
image_files = sorted([f for f in os.listdir(thumbnail_dir_name) if os.path.isfile(os.path.join(thumbnail_dir_name, f))],
key=extract_numbers)
print(image_files)
return templates.TemplateResponse("index.html", {"request": request, "images": image_files, "images_dir": thumbnail_dir_name})
@app.get("/images/{path:path}")
async def serve_image(path: str):
file_path = path
if not os.path.isfile(file_path):
raise HTTPException(status_code=404, detail="File not found")
with open(file_path, "rb") as f:
content = f.read()
return StreamingResponse(io.BytesIO(content), media_type="image/jpeg")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
# For images in images directory: http://127.0.0.1:8000/?images_dir=images
# For another directory other_folder: http://127.0.0.1:8000/?images_dir=other_folder