Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,14 +11,14 @@ TARGET_WIDTH, TARGET_HEIGHT = 2133, 1200
|
|
| 11 |
POS1 = (1032, 0)
|
| 12 |
POS2 = (4666, 0)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
def ensure_dir(path):
|
| 16 |
os.makedirs(path, exist_ok=True)
|
| 17 |
if not os.access(path, os.W_OK):
|
| 18 |
raise RuntimeError(f"❌ 無法寫入:{path}")
|
| 19 |
ensure_dir(OUTPUT_DIR)
|
| 20 |
|
| 21 |
-
# PDF轉圖片
|
| 22 |
def pdf_to_images(pdf_file):
|
| 23 |
images = []
|
| 24 |
with fitz.open(pdf_file.name) as doc:
|
|
@@ -28,14 +28,14 @@ def pdf_to_images(pdf_file):
|
|
| 28 |
images.append(img)
|
| 29 |
return images
|
| 30 |
|
| 31 |
-
# 合成
|
| 32 |
def overlay_image(bg, fg_img, x, y):
|
| 33 |
fg = fg_img.resize((TARGET_WIDTH, TARGET_HEIGHT), resample=Image.BILINEAR)
|
| 34 |
fg = np.array(fg)
|
| 35 |
bg[y:y+TARGET_HEIGHT, x:x+TARGET_WIDTH] = fg[:, :, :3]
|
| 36 |
return bg
|
| 37 |
|
| 38 |
-
#
|
| 39 |
def process_and_combine(images, prefix):
|
| 40 |
bg_pil = Image.open(BG_PATH).convert("RGB")
|
| 41 |
bg = np.array(bg_pil)
|
|
@@ -64,30 +64,25 @@ def process_and_combine(images, prefix):
|
|
| 64 |
return results, ZIP_PATH, f"✅ 共合成 {len(results)} 張圖,已打包 ZIP"
|
| 65 |
|
| 66 |
# 主流程
|
| 67 |
-
def process_inputs(
|
| 68 |
prefix = prefix.strip() or "result_"
|
| 69 |
images = []
|
| 70 |
-
for file in
|
| 71 |
name = file.name.lower()
|
| 72 |
if name.endswith(".pdf"):
|
| 73 |
images.extend(pdf_to_images(file))
|
| 74 |
elif name.endswith((".png", ".jpg", ".jpeg")):
|
| 75 |
-
img = Image.open(file)
|
| 76 |
-
if img.mode != "RGB":
|
| 77 |
-
img = img.convert("RGB")
|
| 78 |
images.append(img)
|
| 79 |
else:
|
| 80 |
raise gr.Error(f"不支援的檔案格式:{name}")
|
| 81 |
return process_and_combine(images, prefix)
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
def ui_interface(files, prefix):
|
| 85 |
-
return process_inputs(files, prefix)
|
| 86 |
-
|
| 87 |
demo = gr.Interface(
|
| 88 |
-
fn=
|
| 89 |
inputs=[
|
| 90 |
-
gr.
|
| 91 |
gr.Textbox(label="檔名前綴(可選)", placeholder="例如:poster_")
|
| 92 |
],
|
| 93 |
outputs=[
|
|
|
|
| 11 |
POS1 = (1032, 0)
|
| 12 |
POS2 = (4666, 0)
|
| 13 |
|
| 14 |
+
# 建立資料夾
|
| 15 |
def ensure_dir(path):
|
| 16 |
os.makedirs(path, exist_ok=True)
|
| 17 |
if not os.access(path, os.W_OK):
|
| 18 |
raise RuntimeError(f"❌ 無法寫入:{path}")
|
| 19 |
ensure_dir(OUTPUT_DIR)
|
| 20 |
|
| 21 |
+
# PDF 轉圖片
|
| 22 |
def pdf_to_images(pdf_file):
|
| 23 |
images = []
|
| 24 |
with fitz.open(pdf_file.name) as doc:
|
|
|
|
| 28 |
images.append(img)
|
| 29 |
return images
|
| 30 |
|
| 31 |
+
# 合成邏輯
|
| 32 |
def overlay_image(bg, fg_img, x, y):
|
| 33 |
fg = fg_img.resize((TARGET_WIDTH, TARGET_HEIGHT), resample=Image.BILINEAR)
|
| 34 |
fg = np.array(fg)
|
| 35 |
bg[y:y+TARGET_HEIGHT, x:x+TARGET_WIDTH] = fg[:, :, :3]
|
| 36 |
return bg
|
| 37 |
|
| 38 |
+
# 主處理流程
|
| 39 |
def process_and_combine(images, prefix):
|
| 40 |
bg_pil = Image.open(BG_PATH).convert("RGB")
|
| 41 |
bg = np.array(bg_pil)
|
|
|
|
| 64 |
return results, ZIP_PATH, f"✅ 共合成 {len(results)} 張圖,已打包 ZIP"
|
| 65 |
|
| 66 |
# 主流程
|
| 67 |
+
def process_inputs(file_list, prefix):
|
| 68 |
prefix = prefix.strip() or "result_"
|
| 69 |
images = []
|
| 70 |
+
for file in file_list:
|
| 71 |
name = file.name.lower()
|
| 72 |
if name.endswith(".pdf"):
|
| 73 |
images.extend(pdf_to_images(file))
|
| 74 |
elif name.endswith((".png", ".jpg", ".jpeg")):
|
| 75 |
+
img = Image.open(file).convert("RGB")
|
|
|
|
|
|
|
| 76 |
images.append(img)
|
| 77 |
else:
|
| 78 |
raise gr.Error(f"不支援的檔案格式:{name}")
|
| 79 |
return process_and_combine(images, prefix)
|
| 80 |
|
| 81 |
+
# UI
|
|
|
|
|
|
|
|
|
|
| 82 |
demo = gr.Interface(
|
| 83 |
+
fn=process_inputs,
|
| 84 |
inputs=[
|
| 85 |
+
gr.Files(label="📎 上傳 PDF 或圖片", file_types=[".pdf", ".png", ".jpg", ".jpeg"]),
|
| 86 |
gr.Textbox(label="檔名前綴(可選)", placeholder="例如:poster_")
|
| 87 |
],
|
| 88 |
outputs=[
|