mylesai commited on
Commit
c6654ae
·
verified ·
1 Parent(s): e9e2262

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -4
app.py CHANGED
@@ -230,6 +230,41 @@ def process_image(image_path):
230
 
231
  return image
232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  # creates the asset in the client's brand folder
234
  def create_asset(client_name, collection_id, image_path, topical_map, tags=True, project_bool=False):
235
  '''
@@ -263,10 +298,7 @@ def create_asset(client_name, collection_id, image_path, topical_map, tags=True,
263
  # container for the uploaded image to be used by the post request
264
  og_object_url = r.json()['object_url']
265
 
266
- # uploads the image
267
- with open(image_path, 'rb') as f:
268
- response = requests.put(upload_url, data=f)
269
-
270
 
271
  # binary upload of image_path
272
  r = requests.get('https://brandfolder.com/api/v4/upload_requests', params={}, headers=headers)
 
230
 
231
  return image
232
 
233
+ def convert_heic_to_jpeg(heic_path):
234
+ # Read the HEIC file
235
+ heif_file = pyheif.read(heic_path)
236
+ # Convert to a PIL image
237
+ image = Image.frombytes(
238
+ heif_file.mode,
239
+ heif_file.size,
240
+ heif_file.data,
241
+ "raw",
242
+ heif_file.mode,
243
+ heif_file.stride,
244
+ )
245
+ # Convert image to JPEG in memory
246
+ jpeg_buffer = BytesIO()
247
+ image.save(jpeg_buffer, format="JPEG")
248
+ jpeg_buffer.seek(0)
249
+ return jpeg_buffer
250
+
251
+ def upload_image(image_path, upload_url):
252
+ # Check if the image is a HEIC file
253
+ if image_path.lower().endswith('.heic'):
254
+ # Convert HEIC to JPEG
255
+ data = convert_heic_to_jpeg(image_path)
256
+ else:
257
+ # Open other image types directly
258
+ data = open(image_path, 'rb')
259
+
260
+ # Upload the image
261
+ response = requests.put(upload_url, data=data)
262
+ # Ensure you close the file stream if opened directly
263
+ if not image_path.lower().endswith('.heic'):
264
+ data.close()
265
+
266
+ return response
267
+
268
  # creates the asset in the client's brand folder
269
  def create_asset(client_name, collection_id, image_path, topical_map, tags=True, project_bool=False):
270
  '''
 
298
  # container for the uploaded image to be used by the post request
299
  og_object_url = r.json()['object_url']
300
 
301
+ response = upload_image(image_path, upload_url)
 
 
 
302
 
303
  # binary upload of image_path
304
  r = requests.get('https://brandfolder.com/api/v4/upload_requests', params={}, headers=headers)