From 2d18258fe386e13dbb3a65393eaec1a3dd8b4408 Mon Sep 17 00:00:00 2001 From: Edward Chang Date: Wed, 22 Apr 2026 18:15:19 +0800 Subject: [PATCH] Update sprite_webtool.py --- sprite_webtool.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/sprite_webtool.py b/sprite_webtool.py index 49f3e20..c7c676d 100644 --- a/sprite_webtool.py +++ b/sprite_webtool.py @@ -37,6 +37,10 @@ HTML = ''' WH: +
+ Out WH: + +
Cols: @@ -189,6 +193,7 @@ async function exportFile(){ const fd = new FormData(); fd.append('file', fileBlob); fd.append('outCols', n('outCols')); + fd.append('outWH', n('outWH')); // 新增這一行 fd.append('name', document.getElementById('name').value); fd.append('picks', JSON.stringify(picks)); @@ -222,6 +227,7 @@ def home(): async def export_picker( file: UploadFile = File(...), outCols: int = Form(...), + outWH: int = Form(...), # 新增參數 name: str = Form(...), picks: str = Form(...) ): @@ -232,31 +238,37 @@ async def export_picker( if not pick_list: return HTMLResponse('None', status_code=400) - tw, th = pick_list[0]['w'], pick_list[0]['h'] + # 輸出尺寸現在統一使用使用者設定的 outWH + tw = th = outWH base_name = os.path.splitext(name)[0] - # 計算總共可以組成幾張完整的圖片 (捨棄餘數) num_picks = len(pick_list) num_images = num_picks // outCols if num_images == 0: return HTMLResponse('選取數量不足以構成一列', status_code=400) - # 儲存產出的路徑清單 generated_files = [] for img_idx in range(num_images): - # 建立單張輸出圖 (高度固定為 1 row) + # 建立畫布,寬度為 (OutWH * Cols),高度為 OutWH out_img = Image.new('RGBA', (outCols * tw, th), (0, 0, 0, 0)) for col_idx in range(outCols): p_idx = img_idx * outCols + col_idx p = pick_list[p_idx] box = (p['x'], p['y'], p['x'] + p['w'], p['y'] + p['h']) + + # 1. 裁切原始大小 tile = img.crop(box) + + # 2. 強制縮放至目標 OutWH + # 使用 Resampling.LANCZOS 保持像素品質,若要保留像素感可用 NEAREST + tile = tile.resize((tw, th), Image.Resampling.LANCZOS) + + # 3. 貼上 out_img.paste(tile, (col_idx * tw, 0)) - # 存檔 file_path = os.path.join(OUTPUT_DIR, f"{base_name}_{img_idx+1}.png") out_img.save(file_path) generated_files.append(file_path)