Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,7 @@ HTML_CONTENT = """
|
|
| 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; }
|
|
@@ -26,6 +27,7 @@ HTML_CONTENT = """
|
|
| 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>
|
|
@@ -41,14 +43,19 @@ def index():
|
|
| 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}")
|
|
@@ -57,9 +64,15 @@ def upload():
|
|
| 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
|
| 63 |
gif_bytes.seek(0)
|
| 64 |
|
| 65 |
return send_file(
|
|
|
|
| 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 |
+
input[type="number"] { margin-bottom: 10px; display: block; margin-left: auto; margin-right: auto; }
|
| 19 |
button { background-color: #007bff; color: white; padding: 10px 15px; border: none; border-radius: 5px; cursor: pointer; }
|
| 20 |
button:hover { background-color: #0056b3; }
|
| 21 |
#gif-container { margin-top: 20px; }
|
|
|
|
| 27 |
<h1>GIF Generator</h1>
|
| 28 |
<form method="post" action="/upload" enctype="multipart/form-data">
|
| 29 |
<input type="file" name="images" accept="image/*" multiple>
|
| 30 |
+
<input type="number" name="fps" placeholder="Frames per second" value="10" min="1" max="60">
|
| 31 |
<button type="submit">Generate GIF</button>
|
| 32 |
</form>
|
| 33 |
</div>
|
|
|
|
| 43 |
def upload():
|
| 44 |
try:
|
| 45 |
files = request.files.getlist('images')
|
| 46 |
+
fps = int(request.form.get('fps', 10))
|
| 47 |
if not files:
|
| 48 |
return "No images uploaded", 400
|
| 49 |
|
| 50 |
images = []
|
| 51 |
+
max_width = 0
|
| 52 |
+
max_height = 0
|
| 53 |
for file in files:
|
| 54 |
if file:
|
| 55 |
try:
|
| 56 |
img = Image.open(BytesIO(file.read()))
|
| 57 |
+
max_width = max(max_width, img.width)
|
| 58 |
+
max_height = max(max_height, img.height)
|
| 59 |
images.append(img)
|
| 60 |
except Exception as e:
|
| 61 |
print(f"Error opening image: {e}")
|
|
|
|
| 64 |
if not images:
|
| 65 |
return "No valid images processed", 400
|
| 66 |
|
| 67 |
+
# Resize images to the largest dimensions
|
| 68 |
+
resized_images = []
|
| 69 |
+
for img in images:
|
| 70 |
+
resized_img = img.resize((max_width, max_height), Image.LANCZOS)
|
| 71 |
+
resized_images.append(resized_img)
|
| 72 |
+
|
| 73 |
# Generate GIF using imageio
|
| 74 |
gif_bytes = BytesIO()
|
| 75 |
+
mimwrite(gif_bytes, [img for img in resized_images], format='GIF', fps=fps)
|
| 76 |
gif_bytes.seek(0)
|
| 77 |
|
| 78 |
return send_file(
|