Snippets

Imagga Example of uploading and tagging an image

Updated by Georgi Kostadinov

File tag_uploaded_image.py Modified

  • Ignore whitespace
  • Hide word diff
     content_id = uploaded_files[0]['id']
 
 # Using the content id and the content parameter,
-# make a GET request to the /tagging upload to get
+# make a GET request to the /tagging endpoint to get
 # image tags
 tagging_query = {'content': content_id}
 tagging_response = requests.get(
Created by Georgi Kostadinov

File tag_uploaded_image.py Added

  • Ignore whitespace
  • Hide word diff
+import requests
+from requests.auth import HTTPBasicAuth
+
+endpoint = 'https://api.imagga.com/v1'
+api_key = 'YOUR_API_KEY'
+api_secret = 'YOUR_API_SECRET'
+
+auth = HTTPBasicAuth(api_key, api_secret)
+
+# Open the desired file
+with open('example_image.jpg', 'r') as image_file:
+    filename = image_file.name
+
+    # Upload the multipart-encoded image with a POST
+    # request to the /content endpoint
+    content_response = requests.post(
+        '%s/content' % 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']
+
+    # Get the content id of the uploaded file
+    content_id = uploaded_files[0]['id']
+
+# Using the content id and the content parameter,
+# make a GET request to the /tagging upload to get
+# image tags
+tagging_query = {'content': content_id}
+tagging_response = requests.get(
+    '%s/tagging' % endpoint,
+    auth=auth,
+    params=tagging_query)
+
+results = tagging_response.json()
+print(results)
HTTPS SSH

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