Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
import gradio as gr
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
-
import numpy as np
|
| 6 |
|
| 7 |
|
| 8 |
def merge_images_to_a4(img1, img2, img3=None):
|
|
@@ -24,7 +23,7 @@ def merge_images_to_a4(img1, img2, img3=None):
|
|
| 24 |
valid_images = [img for img in [img1, img2, img3] if img is not None]
|
| 25 |
|
| 26 |
if len(valid_images) == 0:
|
| 27 |
-
return a4_image
|
| 28 |
|
| 29 |
elif len(valid_images) == 1:
|
| 30 |
# Only 1 image: Center display
|
|
@@ -72,12 +71,7 @@ def merge_images_to_a4(img1, img2, img3=None):
|
|
| 72 |
y = (A4_HEIGHT - img_resized.height) // 2
|
| 73 |
a4_image.paste(img_resized, (x, y))
|
| 74 |
|
| 75 |
-
|
| 76 |
-
jpg_bytes = io.BytesIO()
|
| 77 |
-
a4_image.save(jpg_bytes, format='JPEG', quality=95)
|
| 78 |
-
jpg_bytes.seek(0)
|
| 79 |
-
|
| 80 |
-
return a4_image, jpg_bytes.getvalue()
|
| 81 |
|
| 82 |
|
| 83 |
def resize_to_fit(img, max_width, max_height):
|
|
@@ -108,7 +102,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 108 |
**使用說明:**
|
| 109 |
- 上傳 **2 張圖片** → 左右並排
|
| 110 |
- 上傳 **3 張圖片** → 橫向排列(3 列 1 行,從左到右)
|
| 111 |
-
-
|
| 112 |
"""
|
| 113 |
)
|
| 114 |
|
|
@@ -121,17 +115,16 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 121 |
|
| 122 |
image_output = gr.Image(
|
| 123 |
type="pil",
|
| 124 |
-
label="✅ 合併結果
|
| 125 |
-
interactive=False
|
|
|
|
| 126 |
)
|
| 127 |
|
| 128 |
-
download_file = gr.File(label="📥 下載 JPG 檔案", interactive=False)
|
| 129 |
-
|
| 130 |
# Button click event
|
| 131 |
merge_button.click(
|
| 132 |
fn=merge_images_to_a4,
|
| 133 |
inputs=[image_input1, image_input2, image_input3],
|
| 134 |
-
outputs=
|
| 135 |
)
|
| 136 |
|
| 137 |
gr.Markdown(
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
def merge_images_to_a4(img1, img2, img3=None):
|
|
|
|
| 23 |
valid_images = [img for img in [img1, img2, img3] if img is not None]
|
| 24 |
|
| 25 |
if len(valid_images) == 0:
|
| 26 |
+
return a4_image # Return blank page
|
| 27 |
|
| 28 |
elif len(valid_images) == 1:
|
| 29 |
# Only 1 image: Center display
|
|
|
|
| 71 |
y = (A4_HEIGHT - img_resized.height) // 2
|
| 72 |
a4_image.paste(img_resized, (x, y))
|
| 73 |
|
| 74 |
+
return a4_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
|
| 77 |
def resize_to_fit(img, max_width, max_height):
|
|
|
|
| 102 |
**使用說明:**
|
| 103 |
- 上傳 **2 張圖片** → 左右並排
|
| 104 |
- 上傳 **3 張圖片** → 橫向排列(3 列 1 行,從左到右)
|
| 105 |
+
- 合併後可直接右鍵儲存圖片(JPG 格式,高品質)
|
| 106 |
"""
|
| 107 |
)
|
| 108 |
|
|
|
|
| 115 |
|
| 116 |
image_output = gr.Image(
|
| 117 |
type="pil",
|
| 118 |
+
label="✅ 合併結果 (A4 橫向 - JPG 格式)",
|
| 119 |
+
interactive=False,
|
| 120 |
+
format="jpg" # Force JPG output
|
| 121 |
)
|
| 122 |
|
|
|
|
|
|
|
| 123 |
# Button click event
|
| 124 |
merge_button.click(
|
| 125 |
fn=merge_images_to_a4,
|
| 126 |
inputs=[image_input1, image_input2, image_input3],
|
| 127 |
+
outputs=image_output
|
| 128 |
)
|
| 129 |
|
| 130 |
gr.Markdown(
|