Spaces:
Runtime error
Runtime error
Update bf_trigger.py
Browse files- 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 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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={
|