mylesai commited on
Commit
01a12b9
1 Parent(s): 17811a8

Update bf_trigger.py

Browse files
Files changed (1) hide show
  1. bf_trigger.py +54 -25
bf_trigger.py CHANGED
@@ -375,32 +375,61 @@ def create_ai_asset(asset_dict, topical_map, collection_name, new_imgs, tags=Tru
375
  # container for the uploaded image to be used by the post request
376
  object_url = r.json()['object_url']
377
 
378
- url_response = urllib.request.urlopen(image_url)
379
- img_array = np.array(bytearray(url_response.read()), dtype=np.uint8)
380
- image = cv2.imdecode(img_array, -1)
381
- try:
382
- height, width, c = image.shape
383
- except:
384
- height, width = image.shape
385
- area = width*height
386
- if width > height:
387
- # landscape image
388
- if area > 667000:
389
- image = cv2.resize(image, (1000, 667))
390
- else:
391
- # portrait image
392
- if area > 442236:
393
- image = cv2.resize(image, (548, 807))
394
-
395
- # image = sharpen_image(image)
396
-
397
- with NamedTemporaryFile(delete=True, suffix='.jpg') as temp_image:
398
- # fp = TemporaryFile()
399
- cv2.imwrite(temp_image.name, image)
400
- # fp.seek(0)
401
- response = requests.put(upload_url, data=temp_image)
402
- # fp.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
403
 
 
 
404
  # posts image with image name
405
 
406
  r = requests.post(f'https://brandfolder.com/api/v4/collections/{collection_id}/assets', json={
 
375
  # container for the uploaded image to be used by the post request
376
  object_url = r.json()['object_url']
377
 
378
+ def download_and_resize_image(image_url, upload_url):
379
+ # Fetch the image from the URL
380
+ url_response = urllib.request.urlopen(image_url)
381
+ img_array = np.array(bytearray(url_response.read()), dtype=np.uint8)
382
+
383
+ # Try to decode the image using OpenCV
384
+ image = cv2.imdecode(img_array, -1)
385
+
386
+ # If the image is None, it might be a HEIC file
387
+ if image is None:
388
+ heif_file = read_heif(img_array)
389
+ img = Image.frombytes(
390
+ heif_file.mode,
391
+ heif_file.size,
392
+ heif_file.data,
393
+ "raw",
394
+ heif_file.mode,
395
+ heif_file.stride
396
+ )
397
+ # Convert to RGB
398
+ img = img.convert('RGB')
399
+ # Save to a BytesIO object
400
+ img_bytes = BytesIO()
401
+ img.save(img_bytes, format='JPEG')
402
+ img_bytes.seek(0)
403
+ img_array = np.array(bytearray(img_bytes.read()), dtype=np.uint8)
404
+ # Decode the JPEG image using OpenCV
405
+ image = cv2.imdecode(img_array, -1)
406
+
407
+ # Resize the image based on its dimensions and area
408
+ try:
409
+ height, width, c = image.shape
410
+ except:
411
+ height, width = image.shape
412
+
413
+ area = width * height
414
+ if width > height:
415
+ # Landscape image
416
+ if area > 667000:
417
+ image = cv2.resize(image, (1000, 667))
418
+ else:
419
+ # Portrait image
420
+ if area > 442236:
421
+ image = cv2.resize(image, (548, 807))
422
+
423
+ # Save the image to a temporary file and upload it
424
+ with NamedTemporaryFile(delete=True, suffix='.jpg') as temp_image:
425
+ cv2.imwrite(temp_image.name, image)
426
+ temp_image.seek(0)
427
+ response = requests.put(upload_url, data=temp_image)
428
+
429
+ return response
430
 
431
+ response = download_and_resize_image(image_url, upload_url)
432
+
433
  # posts image with image name
434
 
435
  r = requests.post(f'https://brandfolder.com/api/v4/collections/{collection_id}/assets', json={