-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
66 lines (50 loc) · 1.92 KB
/
app.py
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
import os
import requests
import time
from flask import Flask, render_template, request, redirect, url_for
from PIL import Image
app = Flask(__name__)
def get_file_contents(repo_owner, repo_name, file_path):
api_url = f'https://api.github.com/repos/{repo_owner}/{repo_name}/contents/{file_path}'
response = requests.get(api_url)
if response.status_code == 200:
data = response.json()
content = data.get('content', '')
content = content.encode('ascii')
content = content.decode('base64')
return content
else:
print(f"Error: {response.status_code}")
return None
def get_uploaded_images():
upload_folder = 'static/input'
if os.path.exists(upload_folder):
files = [f for f in os.listdir(upload_folder) if os.path.isfile(os.path.join(upload_folder, f))]
# Sort files based on modification time (newest first)
files.sort(key=lambda x: os.path.getmtime(os.path.join(upload_folder, x)), reverse=True)
return files
else:
return []
@app.route('/')
def index():
uploaded_images = get_uploaded_images()
return render_template('index.html', uploaded_images=uploaded_images)
@app.route('/upload', methods=['POST'])
def upload():
if 'file' not in request.files:
return 'No file part'
file = request.files['file']
if file.filename == '':
return 'No selected file'
if file:
upload_folder = 'static/input'
if not os.path.exists(upload_folder):
os.makedirs(upload_folder)
resized_file_path = os.path.join(upload_folder, 'resized_' + file.filename)
with Image.open(file) as img:
img = img.resize((128, 128))
img.save(resized_file_path)
time.sleep(3)
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)