Zuii commited on
Commit
686b24a
Β·
verified Β·
1 Parent(s): 593aefc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -20,16 +20,18 @@ imgbb_api_key = "YOUR_IMGBB_API_KEY"
20
  def convert_to_png(image):
21
  return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
22
 
23
- # πŸ”₯ Encode image to base64 PNG format
24
- def encode_image(image):
25
- image = convert_to_png(image)
26
- _, buffer = cv2.imencode('.png', image)
27
- return base64.b64encode(buffer).decode('utf-8')
28
-
29
- # πŸ› οΈ Upload images to ImgBB to get permanent URLs
 
 
30
  def upload_image_to_imgbb(image_data):
31
  url = f"https://api.imgbb.com/1/upload?key={imgbb_api_key}"
32
- files = {"image": base64.b64encode(image_data).decode('utf-8')}
33
  response = requests.post(url, files=files)
34
 
35
  if response.status_code == 200:
@@ -50,12 +52,18 @@ def tryon(person_img, garment_img, seed, randomize_seed):
50
  if randomize_seed:
51
  seed = random.randint(0, MAX_SEED)
52
 
53
- # πŸ”₯ Convert images to base64 PNG
54
- _, person_encoded = cv2.imencode('.png', convert_to_png(person_img))
55
- _, garment_encoded = cv2.imencode('.png', convert_to_png(garment_img))
 
 
 
56
 
57
  # βœ… Upload person and garment images to get valid URLs
 
58
  person_url = upload_image_to_imgbb(person_encoded)
 
 
59
  garment_url = upload_image_to_imgbb(garment_encoded)
60
 
61
  if not person_url or not garment_url:
@@ -131,8 +139,6 @@ def tryon(person_img, garment_img, seed, randomize_seed):
131
  return result_img, seed, info
132
 
133
 
134
-
135
-
136
  example_path = os.path.join(os.path.dirname(__file__), 'assets')
137
 
138
  garm_list = os.listdir(os.path.join(example_path,"cloth"))
 
20
  def convert_to_png(image):
21
  return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
22
 
23
+ # πŸ”₯ Resize large images to prevent upload failures (ImgBB limit: 32MB)
24
+ def resize_image(image, max_size=1024):
25
+ height, width = image.shape[:2]
26
+ if max(height, width) > max_size:
27
+ scale = max_size / max(height, width)
28
+ return cv2.resize(image, (int(width * scale), int(height * scale)))
29
+ return image
30
+
31
+ # πŸ› οΈ Upload images to ImgBB (fixed payload!)
32
  def upload_image_to_imgbb(image_data):
33
  url = f"https://api.imgbb.com/1/upload?key={imgbb_api_key}"
34
+ files = {"image": image_data}
35
  response = requests.post(url, files=files)
36
 
37
  if response.status_code == 200:
 
52
  if randomize_seed:
53
  seed = random.randint(0, MAX_SEED)
54
 
55
+ # πŸ”₯ Convert and resize images to base64 PNG
56
+ person_img = resize_image(convert_to_png(person_img))
57
+ garment_img = resize_image(convert_to_png(garment_img))
58
+
59
+ _, person_encoded = cv2.imencode('.png', person_img)
60
+ _, garment_encoded = cv2.imencode('.png', garment_img)
61
 
62
  # βœ… Upload person and garment images to get valid URLs
63
+ print("πŸ“€ Uploading person image...")
64
  person_url = upload_image_to_imgbb(person_encoded)
65
+
66
+ print("πŸ“€ Uploading garment image...")
67
  garment_url = upload_image_to_imgbb(garment_encoded)
68
 
69
  if not person_url or not garment_url:
 
139
  return result_img, seed, info
140
 
141
 
 
 
142
  example_path = os.path.join(os.path.dirname(__file__), 'assets')
143
 
144
  garm_list = os.listdir(os.path.join(example_path,"cloth"))