Update app.py
Browse files
app.py
CHANGED
|
@@ -62,6 +62,31 @@ def upscale_with_clarity(image_path):
|
|
| 62 |
except Exception as e:
|
| 63 |
return None, f"ํ์ง ๊ฐ์ ์ค๋ฅ: {str(e)}"
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
def create_coupang_thumbnail_fixed(image_path):
|
| 66 |
"""๊ณ ์ ๋ ์ค์ ์ผ๋ก ์ฟ ํก ์ธ๋ค์ผ ์์ฑ"""
|
| 67 |
if not REPLICATE_API_TOKEN:
|
|
@@ -82,7 +107,12 @@ def create_coupang_thumbnail_fixed(image_path):
|
|
| 82 |
|
| 83 |
response = requests.get(output)
|
| 84 |
if response.status_code == 200:
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
else:
|
| 87 |
return None, "์ธ๋ค์ผ ์์ฑ ์คํจ"
|
| 88 |
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
return None, f"ํ์ง ๊ฐ์ ์ค๋ฅ: {str(e)}"
|
| 64 |
|
| 65 |
+
def force_white_background(image, threshold=240):
|
| 66 |
+
"""์์ฑ๋ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๊ฐ์ ๋ก ์์ ํ ํฐ์์ผ๋ก ๋ณ๊ฒฝ"""
|
| 67 |
+
try:
|
| 68 |
+
# RGB ๋ชจ๋๋ก ๋ณํ
|
| 69 |
+
if image.mode != 'RGB':
|
| 70 |
+
image = image.convert('RGB')
|
| 71 |
+
|
| 72 |
+
# ์ด๋ฏธ์ง๋ฅผ numpy ๋ฐฐ์ด๋ก ๋ณํ
|
| 73 |
+
img_array = np.array(image)
|
| 74 |
+
|
| 75 |
+
# ๋ฐฐ๊ฒฝ์ผ๋ก ํ๋จ๋๋ ์์ญ ์ฐพ๊ธฐ (๋ฐ์ ์์ ์์ญ)
|
| 76 |
+
# ๊ฐ ํฝ์
์ RGB ๊ฐ์ด ๋ชจ๋ threshold ์ด์์ธ ๊ฒฝ์ฐ ๋ฐฐ๊ฒฝ์ผ๋ก ํ๋จ
|
| 77 |
+
background_mask = np.all(img_array >= threshold, axis=-1)
|
| 78 |
+
|
| 79 |
+
# ๋ฐฐ๊ฒฝ ์์ญ์ ์์ ํ ํฐ์(255, 255, 255)์ผ๋ก ๋ณ๊ฒฝ
|
| 80 |
+
img_array[background_mask] = [255, 255, 255]
|
| 81 |
+
|
| 82 |
+
# numpy ๋ฐฐ์ด์ ๋ค์ PIL ์ด๋ฏธ์ง๋ก ๋ณํ
|
| 83 |
+
result_image = Image.fromarray(img_array)
|
| 84 |
+
|
| 85 |
+
return result_image, "์์ ํ ํฐ์ ๋ฐฐ๊ฒฝ ๊ฐ์ ์ ์ฉ ์๋ฃ"
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
return image, f"๋ฐฐ๊ฒฝ ํฐ์ ๋ณ๊ฒฝ ์คํจ: {str(e)}"
|
| 89 |
+
|
| 90 |
def create_coupang_thumbnail_fixed(image_path):
|
| 91 |
"""๊ณ ์ ๋ ์ค์ ์ผ๋ก ์ฟ ํก ์ธ๋ค์ผ ์์ฑ"""
|
| 92 |
if not REPLICATE_API_TOKEN:
|
|
|
|
| 107 |
|
| 108 |
response = requests.get(output)
|
| 109 |
if response.status_code == 200:
|
| 110 |
+
generated_image = Image.open(io.BytesIO(response.content))
|
| 111 |
+
|
| 112 |
+
# ์์ฑ๋ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๊ฐ์ ๋ก ์์ ํ ํฐ์์ผ๋ก ๋ณ๊ฒฝ
|
| 113 |
+
final_image, white_bg_msg = force_white_background(generated_image)
|
| 114 |
+
|
| 115 |
+
return final_image, f"์ธ๋ค์ผ ์์ฑ ์๋ฃ + {white_bg_msg}"
|
| 116 |
else:
|
| 117 |
return None, "์ธ๋ค์ผ ์์ฑ ์คํจ"
|
| 118 |
|