Update app.py
Browse files
app.py
CHANGED
|
@@ -66,7 +66,63 @@ def upload_image_to_temp_url(image):
|
|
| 66 |
image.save(tmp_file.name, format='PNG')
|
| 67 |
return tmp_file.name
|
| 68 |
|
| 69 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
"""ํฐ ๋ฐฐ๊ฒฝ์ ๊ทธ๋ฆผ์ ํจ๊ณผ ์ถ๊ฐ"""
|
| 71 |
if image.mode != 'RGBA':
|
| 72 |
image = image.convert('RGBA')
|
|
@@ -207,7 +263,7 @@ def process_with_ai_enhancement(image_path, prompt_option):
|
|
| 207 |
except Exception as e:
|
| 208 |
return None, f"AI ์ฒ๋ฆฌ ์ค ์ค๋ฅ: {str(e)}"
|
| 209 |
|
| 210 |
-
def create_coupang_thumbnail(image, auto_remove_bg,
|
| 211 |
"""์ฟ ํก ์ธ๋ค์ผ ์์ฑ ๋ฉ์ธ ํจ์"""
|
| 212 |
if image is None:
|
| 213 |
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์."
|
|
@@ -260,23 +316,30 @@ def create_coupang_thumbnail(image, auto_remove_bg, apply_shadow, apply_angle_ch
|
|
| 260 |
result_image = enhance_image_quality(result_image)
|
| 261 |
status_messages.append("โ
ํ์ง ํฅ์ ์๋ฃ")
|
| 262 |
|
| 263 |
-
# 4๋จ๊ณ:
|
| 264 |
-
if
|
|
|
|
|
|
|
|
|
|
| 265 |
result_image = create_white_background_shadow(result_image)
|
| 266 |
-
status_messages.append("โ
๊ทธ๋ฆผ์
|
| 267 |
-
else:
|
| 268 |
# ๊ทธ๋ฆผ์ ์์ด ํฐ ๋ฐฐ๊ฒฝ๋ง ์ ์ฉ
|
| 269 |
if result_image.mode == 'RGBA':
|
| 270 |
white_bg = Image.new('RGB', result_image.size, (255, 255, 255))
|
| 271 |
white_bg.paste(result_image, mask=result_image.split()[-1] if result_image.mode == 'RGBA' else None)
|
| 272 |
result_image = white_bg
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
# 85% ํฐ์ ๋ฐฐ๊ฒฝ ๊ท์น ํ์ธ
|
| 275 |
is_compliant, white_ratio = check_white_background_ratio(result_image)
|
| 276 |
if is_compliant:
|
| 277 |
status_messages.append(f"โ
85% ํฐ์ ๋ฐฐ๊ฒฝ ๊ท์น ์ค์ ({white_ratio:.1%})")
|
| 278 |
else:
|
| 279 |
-
status_messages.append(f"โ ๏ธ ํฐ์ ๋ฐฐ๊ฒฝ
|
| 280 |
|
| 281 |
# ์ต์ข
์ด๋ฏธ์ง ํฌ๊ธฐ ์กฐ์ (์ฟ ํก ๊ถ์ฅ ์ฌ์ด์ฆ)
|
| 282 |
result_image = result_image.resize((1000, 1000), Image.Resampling.LANCZOS)
|
|
@@ -323,11 +386,12 @@ def create_interface():
|
|
| 323 |
)
|
| 324 |
|
| 325 |
with gr.Group():
|
| 326 |
-
gr.Markdown("**๐จ
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
value=
|
| 330 |
-
|
|
|
|
| 331 |
)
|
| 332 |
apply_angle_change = gr.Checkbox(
|
| 333 |
label="์ํ ๊ฐ๋ ๋ณ๊ฒฝ (์์ดํ
์๋ ํํผ)",
|
|
@@ -409,7 +473,7 @@ def create_interface():
|
|
| 409 |
inputs=[
|
| 410 |
input_image,
|
| 411 |
auto_remove_bg,
|
| 412 |
-
|
| 413 |
apply_angle_change,
|
| 414 |
angle_option,
|
| 415 |
ai_enhancement,
|
|
|
|
| 66 |
image.save(tmp_file.name, format='PNG')
|
| 67 |
return tmp_file.name
|
| 68 |
|
| 69 |
+
def create_perfect_white_background(image, margin=100):
|
| 70 |
+
"""์์ ํ ํฐ์ ๋ฐฐ๊ฒฝ ์์ฑ"""
|
| 71 |
+
if image.mode != 'RGBA':
|
| 72 |
+
image = image.convert('RGBA')
|
| 73 |
+
|
| 74 |
+
# ์๋ณธ ์ด๋ฏธ์ง ํฌ๊ธฐ
|
| 75 |
+
width, height = image.size
|
| 76 |
+
|
| 77 |
+
# ์ฌ๋ฐฑ์ ํฌํจํ ์๋ก์ด ํฌ๊ธฐ
|
| 78 |
+
new_width = width + margin * 2
|
| 79 |
+
new_height = height + margin * 2
|
| 80 |
+
|
| 81 |
+
# ์์ ํ ํฐ์ ๋ฐฐ๊ฒฝ ์์ฑ
|
| 82 |
+
white_background = Image.new('RGB', (new_width, new_height), (255, 255, 255))
|
| 83 |
+
|
| 84 |
+
# ์๋ณธ ์ด๋ฏธ์ง๋ฅผ ์ค์์ ๋ฐฐ์น
|
| 85 |
+
paste_x = (new_width - width) // 2
|
| 86 |
+
paste_y = (new_height - height) // 2
|
| 87 |
+
|
| 88 |
+
# ์ํ ์ฑ๋์ด ์๋ ๊ฒฝ์ฐ ํฌ๋ช
๋ถ๋ถ ์ฒ๋ฆฌ
|
| 89 |
+
if image.mode == 'RGBA':
|
| 90 |
+
# ํฌ๋ช
๋ฐฐ๊ฒฝ์ ํฐ์์ผ๋ก ๋ณ๊ฒฝ
|
| 91 |
+
white_bg_temp = Image.new('RGBA', image.size, (255, 255, 255, 255))
|
| 92 |
+
alpha_composite = Image.alpha_composite(white_bg_temp, image)
|
| 93 |
+
white_background.paste(alpha_composite.convert('RGB'), (paste_x, paste_y))
|
| 94 |
+
else:
|
| 95 |
+
white_background.paste(image, (paste_x, paste_y))
|
| 96 |
+
|
| 97 |
+
return white_background
|
| 98 |
+
|
| 99 |
+
def ensure_white_background_85_percent(image):
|
| 100 |
+
"""85% ํฐ์ ๋ฐฐ๊ฒฝ ๊ท์น์ ํ์คํ ๋ง์กฑ์ํค๊ธฐ"""
|
| 101 |
+
# ํ์ฌ ํฐ์ ๋น์จ ํ์ธ
|
| 102 |
+
is_compliant, white_ratio = check_white_background_ratio(image)
|
| 103 |
+
|
| 104 |
+
if not is_compliant:
|
| 105 |
+
# 85% ๋ฏธ๋ฌ์ ์ด๋ฏธ์ง๋ฅผ ์ถ์ํ์ฌ ํฐ์ ์์ญ ๋๋ฆฌ๊ธฐ
|
| 106 |
+
current_size = image.size
|
| 107 |
+
# ํ์ฌ ํฐ์ ๋น์จ์ ๋ฐ๋ผ ์ถ์ ๋น์จ ๊ณ์ฐ
|
| 108 |
+
target_ratio = 0.87 # 85%๋ณด๋ค ์ฝ๊ฐ ์ฌ์ ์๊ฒ
|
| 109 |
+
if white_ratio > 0:
|
| 110 |
+
scale_factor = min(0.8, (white_ratio / target_ratio) * 0.9)
|
| 111 |
+
else:
|
| 112 |
+
scale_factor = 0.7
|
| 113 |
+
|
| 114 |
+
new_size = (int(current_size[0] * scale_factor), int(current_size[1] * scale_factor))
|
| 115 |
+
resized_image = image.resize(new_size, Image.Resampling.LANCZOS)
|
| 116 |
+
|
| 117 |
+
# ์์ ํ ํฐ์ ๋ฐฐ๊ฒฝ์ ์ถ์๋ ์ด๋ฏธ์ง๋ฅผ ์ค์ ๋ฐฐ์น
|
| 118 |
+
final_image = Image.new('RGB', current_size, (255, 255, 255))
|
| 119 |
+
paste_x = (current_size[0] - new_size[0]) // 2
|
| 120 |
+
paste_y = (current_size[1] - new_size[1]) // 2
|
| 121 |
+
final_image.paste(resized_image, (paste_x, paste_y))
|
| 122 |
+
|
| 123 |
+
return final_image
|
| 124 |
+
|
| 125 |
+
return image
|
| 126 |
"""ํฐ ๋ฐฐ๊ฒฝ์ ๊ทธ๋ฆผ์ ํจ๊ณผ ์ถ๊ฐ"""
|
| 127 |
if image.mode != 'RGBA':
|
| 128 |
image = image.convert('RGBA')
|
|
|
|
| 263 |
except Exception as e:
|
| 264 |
return None, f"AI ์ฒ๋ฆฌ ์ค ์ค๋ฅ: {str(e)}"
|
| 265 |
|
| 266 |
+
def create_coupang_thumbnail(image, auto_remove_bg, white_bg_mode, apply_angle_change, angle_option, ai_enhancement, prompt_option):
|
| 267 |
"""์ฟ ํก ์ธ๋ค์ผ ์์ฑ ๋ฉ์ธ ํจ์"""
|
| 268 |
if image is None:
|
| 269 |
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์."
|
|
|
|
| 316 |
result_image = enhance_image_quality(result_image)
|
| 317 |
status_messages.append("โ
ํ์ง ํฅ์ ์๋ฃ")
|
| 318 |
|
| 319 |
+
# 4๋จ๊ณ: ๋ฐฐ๊ฒฝ ์ฒ๋ฆฌ (ํฐ์ ๋ฐฐ๊ฒฝ ๋ชจ๋ ์ ํ)
|
| 320 |
+
if white_bg_mode == "์์ ํ ํฐ์":
|
| 321 |
+
result_image = create_perfect_white_background(result_image)
|
| 322 |
+
status_messages.append("โ
์์ ํ ํฐ์ ๋ฐฐ๊ฒฝ ์ ์ฉ")
|
| 323 |
+
elif white_bg_mode == "๊ทธ๋ฆผ์ ํจ๊ณผ":
|
| 324 |
result_image = create_white_background_shadow(result_image)
|
| 325 |
+
status_messages.append("โ
๊ทธ๋ฆผ์ ํจ๊ณผ๊ฐ ์๋ ํฐ์ ๋ฐฐ๊ฒฝ ์ ์ฉ")
|
| 326 |
+
else: # "๊ธฐ๋ณธ ํฐ์"
|
| 327 |
# ๊ทธ๋ฆผ์ ์์ด ํฐ ๋ฐฐ๊ฒฝ๋ง ์ ์ฉ
|
| 328 |
if result_image.mode == 'RGBA':
|
| 329 |
white_bg = Image.new('RGB', result_image.size, (255, 255, 255))
|
| 330 |
white_bg.paste(result_image, mask=result_image.split()[-1] if result_image.mode == 'RGBA' else None)
|
| 331 |
result_image = white_bg
|
| 332 |
+
status_messages.append("โ
๊ธฐ๋ณธ ํฐ์ ๋ฐฐ๊ฒฝ ์ ์ฉ")
|
| 333 |
+
|
| 334 |
+
# 5๋จ๊ณ: 85% ํฐ์ ๋ฐฐ๊ฒฝ ๊ท์น ๊ฐ์ ์ ์ฉ
|
| 335 |
+
result_image = ensure_white_background_85_percent(result_image)
|
| 336 |
|
| 337 |
# 85% ํฐ์ ๋ฐฐ๊ฒฝ ๊ท์น ํ์ธ
|
| 338 |
is_compliant, white_ratio = check_white_background_ratio(result_image)
|
| 339 |
if is_compliant:
|
| 340 |
status_messages.append(f"โ
85% ํฐ์ ๋ฐฐ๊ฒฝ ๊ท์น ์ค์ ({white_ratio:.1%})")
|
| 341 |
else:
|
| 342 |
+
status_messages.append(f"โ ๏ธ ํฐ์ ๋ฐฐ๊ฒฝ ๋น์จ: ({white_ratio:.1%}) - ์ถ๊ฐ ์กฐ์ ํ์")
|
| 343 |
|
| 344 |
# ์ต์ข
์ด๋ฏธ์ง ํฌ๊ธฐ ์กฐ์ (์ฟ ํก ๊ถ์ฅ ์ฌ์ด์ฆ)
|
| 345 |
result_image = result_image.resize((1000, 1000), Image.Resampling.LANCZOS)
|
|
|
|
| 386 |
)
|
| 387 |
|
| 388 |
with gr.Group():
|
| 389 |
+
gr.Markdown("**๐จ ๋ฐฐ๊ฒฝ ์ต์
**")
|
| 390 |
+
white_bg_mode = gr.Radio(
|
| 391 |
+
choices=["์์ ํ ํฐ์", "๊ทธ๋ฆผ์ ํจ๊ณผ", "๊ธฐ๋ณธ ํฐ์"],
|
| 392 |
+
value="์์ ํ ํฐ์",
|
| 393 |
+
label="ํฐ์ ๋ฐฐ๊ฒฝ ๋ชจ๋",
|
| 394 |
+
info="์์ ํ ํฐ์: 100% ์๋ฐฑ์ / ๊ทธ๋ฆผ์ ํจ๊ณผ: ์
์ฒด๊ฐ ์ฐ์ถ / ๊ธฐ๋ณธ ํฐ์: ๋จ์ ๋ฐฐ๊ฒฝ"
|
| 395 |
)
|
| 396 |
apply_angle_change = gr.Checkbox(
|
| 397 |
label="์ํ ๊ฐ๋ ๋ณ๊ฒฝ (์์ดํ
์๋ ํํผ)",
|
|
|
|
| 473 |
inputs=[
|
| 474 |
input_image,
|
| 475 |
auto_remove_bg,
|
| 476 |
+
white_bg_mode,
|
| 477 |
apply_angle_change,
|
| 478 |
angle_option,
|
| 479 |
ai_enhancement,
|