yangtb24 commited on
Commit
98ba7ad
·
verified ·
1 Parent(s): 821b43d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from flask import Flask, request, send_file
3
+ from io import BytesIO
4
+ from PIL import Image
5
+ from imageio import mimwrite
6
+
7
+ app = Flask(__name__)
8
+
9
+ HTML_CONTENT = """
10
+ <!DOCTYPE html>
11
+ <html>
12
+ <head>
13
+ <title>GIF Generator</title>
14
+ <style>
15
+ body { font-family: sans-serif; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; background-color: #f4f4f4; }
16
+ .container { background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); text-align: center; }
17
+ input[type="file"] { margin-bottom: 10px; display: block; margin-left: auto; margin-right: auto; }
18
+ button { background-color: #007bff; color: white; padding: 10px 15px; border: none; border-radius: 5px; cursor: pointer; }
19
+ button:hover { background-color: #0056b3; }
20
+ #gif-container { margin-top: 20px; }
21
+ #gif-image { max-width: 100%; }
22
+ </style>
23
+ </head>
24
+ <body>
25
+ <div class="container">
26
+ <h1>GIF Generator</h1>
27
+ <form method="post" action="/upload" enctype="multipart/form-data">
28
+ <input type="file" name="images" accept="image/*" multiple>
29
+ <button type="submit">Generate GIF</button>
30
+ </form>
31
+ </div>
32
+ </body>
33
+ </html>
34
+ """
35
+
36
+ @app.route('/', methods=['GET'])
37
+ def index():
38
+ return HTML_CONTENT
39
+
40
+ @app.route('/upload', methods=['POST'])
41
+ def upload():
42
+ try:
43
+ files = request.files.getlist('images')
44
+ if not files:
45
+ return "No images uploaded", 400
46
+
47
+ images = []
48
+ for file in files:
49
+ if file:
50
+ try:
51
+ img = Image.open(BytesIO(file.read()))
52
+ images.append(img)
53
+ except Exception as e:
54
+ print(f"Error opening image: {e}")
55
+ return f"Error opening image: {e}", 500
56
+
57
+ if not images:
58
+ return "No valid images processed", 400
59
+
60
+ # Generate GIF using imageio
61
+ gif_bytes = BytesIO()
62
+ mimwrite(gif_bytes, [img for img in images], format='GIF', fps=10)
63
+ gif_bytes.seek(0)
64
+
65
+ return send_file(
66
+ gif_bytes,
67
+ mimetype='image/gif',
68
+ download_name='animated.gif'
69
+ )
70
+
71
+ except Exception as e:
72
+ print(f"Error during upload: {e}")
73
+ return f"Error: {e}", 500
74
+
75
+ if __name__ == '__main__':
76
+ app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)), debug=True)