ssboost commited on
Commit
70373d7
ยท
verified ยท
1 Parent(s): 70309ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -1
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
- return Image.open(io.BytesIO(response.content)), "์ธ๋„ค์ผ ์ƒ์„ฑ ์™„๋ฃŒ"
 
 
 
 
 
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