tunght commited on
Commit
a549c57
·
1 Parent(s): aac4967

add resize image

Browse files
Files changed (1) hide show
  1. app.py +24 -3
app.py CHANGED
@@ -419,13 +419,33 @@ Return the product description only.
419
 
420
  import base64
421
  import requests
 
 
422
 
423
  # OpenAI API Key
424
 
425
  # Function to encode the image
426
- def encode_image(image_path):
427
- with open(image_path, "rb") as image_file:
428
- return base64.b64encode(image_file.read()).decode('utf-8')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
429
 
430
 
431
  import json
@@ -478,6 +498,7 @@ def detect_features(image_paths, garment_type, language="English"):
478
  },
479
  ] + [{
480
  "type": "image_url",
 
481
  "image_url": {
482
  "url": f"data:image/jpeg;base64,{base64_image}"
483
  }
 
419
 
420
  import base64
421
  import requests
422
+ from PIL import Image
423
+ import io
424
 
425
  # OpenAI API Key
426
 
427
  # Function to encode the image
428
+ # def encode_image(image_path):
429
+ # with open(image_path, "rb") as image_file:
430
+ # return base64.b64encode(image_file.read()).decode('utf-8')
431
+
432
+
433
+ def encode_image(image_path, img_size=512):
434
+ with open(image_path, "rb") as image_file:
435
+ # b64_img = base64.b64encode(image_file.read()).decode('utf-8')
436
+ # b64_img = base64.b64decode(base64.b64encode(response.content))
437
+ img = Image.open(image_file)
438
+ w, h = img.size
439
+ max_size = max(w, h)
440
+ if max_size > img_size:
441
+ ratio = img_size / max_size
442
+ print("Original size: ", w, h)
443
+ w, h = int(w * ratio), int(h * ratio)
444
+ print("Resizing image to ", w, h)
445
+ img = img.resize(size=(w, h))
446
+ buffer = io.BytesIO()
447
+ img.save(buffer, format="PNG")
448
+ return base64.b64encode(buffer.getvalue()).decode('utf-8')
449
 
450
 
451
  import json
 
498
  },
499
  ] + [{
500
  "type": "image_url",
501
+ "detail": "low",
502
  "image_url": {
503
  "url": f"data:image/jpeg;base64,{base64_image}"
504
  }