Spaces:
Sleeping
Sleeping
File size: 14,258 Bytes
4616fe0 34acaea 4616fe0 34acaea 4616fe0 34acaea 4616fe0 8cbf42a 34acaea 8cbf42a 34acaea 8cbf42a 34acaea 8cbf42a 34acaea 8cbf42a 34acaea 8cbf42a 34acaea 8cbf42a 34acaea 4616fe0 8cbf42a 4616fe0 8cbf42a 4616fe0 34acaea 8cbf42a 34acaea 4616fe0 8cbf42a 34acaea 4616fe0 34acaea 4616fe0 8cbf42a 4616fe0 8cbf42a 34acaea 8cbf42a 4616fe0 8cbf42a 34acaea 4616fe0 8cbf42a 4616fe0 8cbf42a 4616fe0 8cbf42a 34acaea 8cbf42a 34acaea 8cbf42a 34acaea 8cbf42a 34acaea 8cbf42a 34acaea 8cbf42a 34acaea 8cbf42a 34acaea 8cbf42a 4616fe0 8cbf42a 4616fe0 8cbf42a 4616fe0 8cbf42a 4616fe0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
import gradio as gr
import numpy as np
from PIL import Image
import io
import base64
import tempfile
import os
def calculate_crop_box(image, target_ratio=1170/391):
"""
計算保持目標比例的最大裁切框
"""
width, height = image.size
current_ratio = width / height
if current_ratio > target_ratio:
# 圖片較寬,需要裁切寬度
new_width = int(height * target_ratio)
left = (width - new_width) // 2
top = 0
right = left + new_width
bottom = height
else:
# 圖片較高,需要裁切高度
new_height = int(width / target_ratio)
left = 0
top = (height - new_height) // 2
right = width
bottom = top + new_height
return (left, top, right, bottom)
def process_image(image, crop_data=None):
"""
處理圖片:裁切並調整為目標尺寸,並保存為 JPG 格式
"""
if image is None:
return None, "請上傳圖片"
# 如果有裁切數據,使用用戶選擇的區域
if crop_data is not None:
# Gradio 的 Image 組件在編輯模式下會返回包含裁切信息的數據
# 這裡我們處理裁切後的圖片
processed_image = image
else:
# 如果沒有裁切數據,自動計算最佳裁切區域
crop_box = calculate_crop_box(image)
processed_image = image.crop(crop_box)
# 調整為目標尺寸
target_size = (1170, 391)
final_image = processed_image.resize(target_size, Image.Resampling.LANCZOS)
# 轉換為 RGB 模式以確保 JPG 格式輸出
if final_image.mode in ('RGBA', 'LA', 'P'):
# 創建白色背景
rgb_image = Image.new('RGB', final_image.size, (255, 255, 255))
if final_image.mode == 'P':
final_image = final_image.convert('RGBA')
rgb_image.paste(final_image, mask=final_image.split()[-1] if final_image.mode == 'RGBA' else None)
final_image = rgb_image
# 保存為 JPG 格式的臨時檔案
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.jpg')
final_image.save(temp_file.name, format='JPEG', quality=95, optimize=True)
temp_file.close()
return temp_file.name, f"圖片已處理完成!尺寸:{final_image.size},格式:JPG"
def auto_crop_preview(image):
"""
自動裁切預覽
"""
if image is None:
return None, "請上傳圖片"
crop_box = calculate_crop_box(image)
cropped = image.crop(crop_box)
# 創建預覽圖片,顯示裁切區域
preview = image.copy()
from PIL import ImageDraw
draw = ImageDraw.Draw(preview)
# 繪製裁切框
draw.rectangle(crop_box, outline="red", width=3)
return preview, f"建議裁切區域:{crop_box}"
def create_crop_interface(image):
"""
創建裁切界面,顯示帶有裁切框的圖片
"""
if image is None:
return None, "請先上傳圖片"
# 計算建議的裁切區域
crop_box = calculate_crop_box(image)
left, top, right, bottom = crop_box
# 創建帶有裁切框的預覽圖
preview = image.copy()
from PIL import ImageDraw, ImageFont
draw = ImageDraw.Draw(preview)
# 繪製裁切框
draw.rectangle(crop_box, outline="red", width=5)
# 添加半透明遮罩到裁切區域外
overlay = Image.new('RGBA', image.size, (0, 0, 0, 100))
mask = Image.new('RGBA', image.size, (0, 0, 0, 0))
mask_draw = ImageDraw.Draw(mask)
mask_draw.rectangle(crop_box, fill=(255, 255, 255, 255))
# 創建最終預覽
preview = Image.alpha_composite(preview.convert('RGBA'), overlay)
preview = Image.alpha_composite(preview, Image.new('RGBA', image.size, (0, 0, 0, 0)))
return preview.convert('RGB'), f"建議裁切區域:{crop_box}\n可以點擊「使用建議區域」或「自定義裁切」"
def custom_crop_with_coordinates(image, x, y, width, height):
"""
使用自定義座標裁切圖片
"""
if image is None:
return None, "請先上傳圖片"
try:
# 確保座標在圖片範圍內
img_width, img_height = image.size
x = max(0, min(x, img_width))
y = max(0, min(y, img_height))
width = max(10, min(width, img_width - x))
height = max(10, min(height, img_height - y))
# 執行裁切
cropped = image.crop((x, y, x + width, y + height))
# 調整為目標尺寸
target_size = (1170, 391)
final_image = cropped.resize(target_size, Image.Resampling.LANCZOS)
# 轉換為 RGB 並保存為 JPG
if final_image.mode in ('RGBA', 'LA', 'P'):
rgb_image = Image.new('RGB', final_image.size, (255, 255, 255))
if final_image.mode == 'P':
final_image = final_image.convert('RGBA')
rgb_image.paste(final_image, mask=final_image.split()[-1] if final_image.mode == 'RGBA' else None)
final_image = rgb_image
# 保存為 JPG 檔案
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.jpg')
final_image.save(temp_file.name, format='JPEG', quality=95, optimize=True)
temp_file.close()
return temp_file.name, f"自定義裁切完成!裁切區域:({x}, {y}, {width}, {height})"
except Exception as e:
return None, f"裁切失敗:{str(e)}"
def use_suggested_crop(image):
"""
使用建議的裁切區域
"""
if image is None:
return None, "請先上傳圖片"
crop_box = calculate_crop_box(image)
cropped = image.crop(crop_box)
# 調整為目標尺寸
target_size = (1170, 391)
final_image = cropped.resize(target_size, Image.Resampling.LANCZOS)
# 轉換為 RGB 並保存為 JPG
if final_image.mode in ('RGBA', 'LA', 'P'):
rgb_image = Image.new('RGB', final_image.size, (255, 255, 255))
if final_image.mode == 'P':
final_image = final_image.convert('RGBA')
rgb_image.paste(final_image, mask=final_image.split()[-1] if final_image.mode == 'RGBA' else None)
final_image = rgb_image
# 保存為 JPG 檔案
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.jpg')
final_image.save(temp_file.name, format='JPEG', quality=95, optimize=True)
temp_file.close()
return temp_file.name, f"使用建議裁切區域完成!區域:{crop_box}"
# 創建 Gradio 界面
with gr.Blocks(title="圖片裁切工具", theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🖼️ 圖片裁切工具")
gr.Markdown("### 將任意尺寸的圖片按 1170:391 比例裁切並調整為目標尺寸")
with gr.Row():
with gr.Column(scale=1):
# 圖片上傳區域
input_image = gr.Image(
label="上傳圖片",
type="pil",
height=400,
interactive=True
)
# 手動裁切圖片區域
crop_image = gr.Image(
label="手動裁切區域(點擊並拖拽選擇裁切區域)",
type="pil",
height=400,
interactive=True,
visible=False
)
# 按鈕區域
with gr.Row():
preview_btn = gr.Button("🔍 顯示建議裁切區域", variant="secondary")
suggest_btn = gr.Button("✅ 使用建議區域", variant="primary")
with gr.Row():
edit_btn = gr.Button("✏️ 自定義裁切", variant="secondary")
reset_btn = gr.Button("🔄 重置", variant="secondary")
with gr.Column(scale=1):
# 輸出區域
output_image = gr.Image(
label="處理後圖片 (JPG格式)",
type="filepath",
height=400,
interactive=False
)
# 下載按鈕
download_btn = gr.DownloadButton(
label="📥 下載 JPG 圖片",
variant="primary",
visible=False
)
# 狀態信息
status_text = gr.Textbox(
label="狀態",
value="等待圖片上傳...",
interactive=False
)
# 預覽功能
with gr.Row():
preview_image = gr.Image(
label="裁切區域預覽(紅框顯示建議裁切區域)",
type="pil",
height=300,
visible=False
)
# 使用說明
with gr.Accordion("📖 使用說明", open=False):
gr.Markdown("""
### 如何使用:
1. **上傳圖片**:點擊上傳區域選擇您的圖片
2. **查看建議**:點擊「顯示建議裁切區域」查看系統建議
3. **選擇方式**:
- 點擊「使用建議區域」直接使用系統建議
- 點擊「自定義裁切」手動輸入座標和尺寸
4. **下載結果**:點擊「下載 JPG 圖片」按鈕下載處理後的圖片
### 功能特色:
- ✨ 自動計算最佳裁切比例 (1170:391)
- 🎯 支援自定義座標裁切
- 📏 保持目標比例的精確輸出
- 🖼️ 輸出高品質 JPG 格式圖片
- 💾 簡單的一鍵下載功能
- 🔄 重置功能方便重新開始
### 自定義裁切說明:
- **X, Y 座標**:裁切區域的左上角位置
- **寬度, 高度**:裁切區域的大小
- 最終輸出會調整為 1170×391 像素
""")
# 自定義裁切參數區域
with gr.Row(visible=False) as custom_crop_row:
gr.Markdown("### 🎯 自定義裁切參數")
with gr.Row(visible=False) as crop_controls:
with gr.Column():
crop_x = gr.Number(
label="X 座標 (左上角)",
value=0,
precision=0,
minimum=0
)
crop_y = gr.Number(
label="Y 座標 (左上角)",
value=0,
precision=0,
minimum=0
)
with gr.Column():
crop_width = gr.Number(
label="寬度",
value=300,
precision=0,
minimum=10
)
crop_height = gr.Number(
label="高度",
value=100,
precision=0,
minimum=10
)
with gr.Column():
apply_crop_btn = gr.Button("🎯 套用自定義裁切", variant="primary")
# 事件綁定
# 顯示建議裁切區域
preview_btn.click(
fn=create_crop_interface,
inputs=[input_image],
outputs=[preview_image, status_text]
).then(
fn=lambda: gr.update(visible=True),
outputs=[preview_image]
)
# 使用建議區域直接處理
def suggest_and_download(image):
if image is None:
return None, "請先上傳圖片", gr.update(visible=False)
result_path, status = use_suggested_crop(image)
if result_path:
return result_path, status, gr.update(visible=True, value=result_path)
else:
return None, status, gr.update(visible=False)
suggest_btn.click(
fn=suggest_and_download,
inputs=[input_image],
outputs=[output_image, status_text, download_btn]
)
# 顯示/隱藏自定義裁切控制項
def toggle_custom_controls(image):
if image is None:
return gr.update(visible=False), gr.update(visible=False)
# 獲取圖片尺寸以設定預設值
width, height = image.size
suggested_crop = calculate_crop_box(image)
return (
gr.update(visible=True),
gr.update(visible=True)
)
edit_btn.click(
fn=toggle_custom_controls,
inputs=[input_image],
outputs=[custom_crop_row, crop_controls]
)
# 套用自定義裁切
def apply_custom_crop(image, x, y, width, height):
if image is None:
return None, "請先上傳圖片", gr.update(visible=False)
result_path, status = custom_crop_with_coordinates(image, x, y, width, height)
if result_path:
return result_path, status, gr.update(visible=True, value=result_path)
else:
return None, status, gr.update(visible=False)
apply_crop_btn.click(
fn=apply_custom_crop,
inputs=[input_image, crop_x, crop_y, crop_width, crop_height],
outputs=[output_image, status_text, download_btn]
)
# 重置功能
reset_btn.click(
fn=lambda: (
None, None, None,
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
gr.update(visible=False),
"請上傳圖片"
),
outputs=[
input_image, crop_image, output_image,
preview_image, custom_crop_row, crop_controls,
download_btn, status_text
]
)
# 當圖片上傳時自動更新狀態和建議值
def update_on_upload(image):
if image is None:
return "請上傳圖片", 0, 0, 300, 100
# 計算建議的裁切區域
crop_box = calculate_crop_box(image)
left, top, right, bottom = crop_box
width = right - left
height = bottom - top
return (
f"圖片已上傳!尺寸:{image.size[0]}×{image.size[1]}",
left, top, width, height
)
input_image.change(
fn=update_on_upload,
inputs=[input_image],
outputs=[status_text, crop_x, crop_y, crop_width, crop_height]
)
# 啟動應用程式
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=True
) |