Snippets

Imagga Batch image tagging

Updated by Emil Dimitrov

File tag_images.py Modified

  • Ignore whitespace
  • Hide word diff
 
 ENDPOINT = 'https://api.imagga.com/v2'
 
-FILE_TYPES = ['png', 'jpg', 'jpeg', 'bmp']
+FILE_TYPES = ['png', 'jpg', 'jpeg', 'gif']
 
 
 class ArgumentException(Exception):
         content_response = requests.post(
             '%s/uploads' % ENDPOINT,
             auth=auth,
-            files={filename: image_file})
+            files={'image': image_file})
 
         # Example /uploads response:
         #    {
             image_path = os.path.join(tag_input, image_file)
             print('[%s / %s] %s uploading' %
                   (iterator + 1, images_count, image_path))
-            try:
-                upload_id = upload_image(image_path)
-            except IndexError:
-                continue
-            except KeyError:
-                continue
-            except ArgumentException:
-                continue
+            #try:
+            upload_id = upload_image(image_path)
+            #except IndexError:
+            #    continue
+            #except KeyError:
+            #    continue
+            #except ArgumentException:
+            #    continue
 
             tag_result = tag_image(upload_id, True, verbose, language)
+			
             if not include_colors:
                 results[image_file] = tag_result
             else:
     if merged_output:
         with open(
             os.path.join(tag_output, 'results.json'),
-                'w') as results_file:
+                'wb') as results_file:
             results_file.write(
                 json.dumps(
                     results, ensure_ascii=False, indent=4).encode('utf-8'))
     else:
-        for image, result in results.iteritems():
+        for image, result in results.items():
             with open(
                 os.path.join(tag_output, 'result_%s.json' % image),
-                    'w') as results_file:
+                    'wb') as results_file:
                 results_file.write(
                     json.dumps(
                         result, ensure_ascii=False, indent=4).encode('utf-8'))
 
 
 if __name__ == '__main__':
-    main()
+    main()
Updated by Georgi Kostadinov

File tag_images.py Modified

  • Ignore whitespace
  • Hide word diff
 
 ENDPOINT = 'https://api.imagga.com/v2'
 
-FILE_TYPES = ['png', 'jpg', 'jpeg', 'gif']
+FILE_TYPES = ['png', 'jpg', 'jpeg', 'bmp']
 
 
 class ArgumentException(Exception):
Updated by Georgi Kostadinov

File tag_images.py Modified

  • Ignore whitespace
  • Hide word diff
 
 def extract_colors(image, upload_id=False):
     colors_query = {
-        'image_upoad_id' if upload_id else 'image_url': image,
+        'image_upload_id' if upload_id else 'image_url': image,
     }
 
     colors_response = requests.get(
Updated by Georgi Kostadinov

File tag_images.py Modified

  • Ignore whitespace
  • Hide word diff
                             'YOUR_API_SECRET')  # Set API secret here
 ###
 
-ENDPOINT = 'https://api.imagga.com/v1'
+ENDPOINT = 'https://api.imagga.com/v2'
 
 FILE_TYPES = ['png', 'jpg', 'jpeg', 'gif']
 
         filename = image_file.name
 
         # Upload the multipart-encoded image with a POST
-        # request to the /content endpoint
+        # request to the /uploads endpoint
         content_response = requests.post(
-            '%s/content' % ENDPOINT,
+            '%s/uploads' % ENDPOINT,
             auth=auth,
             files={filename: image_file})
 
-        # Example /content response:
-        # {'status': 'success',
-        #  'uploaded': [{'id': '8aa6e7f083c628407895eb55320ac5ad',
-        #                'filename': 'example_image.jpg'}]}
-        uploaded_files = content_response.json()['uploaded']
+        # Example /uploads response:
+        #    {
+        #      "result": {
+        #        "upload_id": "i05e132196706b94b1d85efb5f3SaM1j"
+        #      },
+        #      "status": {
+        #        "text": "",
+        #        "type": "success"
+        #      }
+        #    }
+        uploaded_file = content_response.json()['result']
 
-        # Get the content id of the uploaded file
-        content_id = uploaded_files[0]['id']
+        # Get the upload id of the uploaded file
+        upload_id = uploaded_file['upload_id']
 
-    return content_id
+    return upload_id
 
 
-def tag_image(image, content_id=False, verbose=False, language='en'):
+def tag_image(image, upload_id=False, verbose=False, language='en'):
     # Using the content id and the content parameter,
     # make a GET request to the /tagging endpoint to get
     # image tags
     tagging_query = {
-        'content' if content_id else 'url': image,
+        'image_upload_id' if upload_id else 'image_url': image,
         'verbose': verbose,
         'language': language
     }
     tagging_response = requests.get(
-        '%s/tagging' % ENDPOINT,
+        '%s/tags' % ENDPOINT,
         auth=auth,
         params=tagging_query)
 
     return tagging_response.json()
 
 
-def extract_colors(image, content_id=False):
+def extract_colors(image, upload_id=False):
     colors_query = {
-        'content' if content_id else 'url': image,
+        'image_upoad_id' if upload_id else 'image_url': image,
     }
 
     colors_response = requests.get(
             print('[%s / %s] %s uploading' %
                   (iterator + 1, images_count, image_path))
             try:
-                content_id = upload_image(image_path)
+                upload_id = upload_image(image_path)
             except IndexError:
                 continue
             except KeyError:
             except ArgumentException:
                 continue
 
-            tag_result = tag_image(content_id, True, verbose, language)
+            tag_result = tag_image(upload_id, True, verbose, language)
             if not include_colors:
                 results[image_file] = tag_result
             else:
-                colors_result = extract_colors(content_id, True)
+                colors_result = extract_colors(upload_id, True)
                 results[image_file] = {
                     'tagging': tag_result,
                     'colors': colors_result
Updated by Ivan Penchev

File tag_images.py Modified

  • Ignore whitespace
  • Hide word diff
         raise ArgumentException('Invalid image path')
 
     # Open the desired file
-    with open(image_path, 'r') as image_file:
+    with open(image_path, 'rb') as image_file:
         filename = image_file.name
 
         # Upload the multipart-encoded image with a POST
  1. 1
  2. 2
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.