+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)
+class ArgumentException(Exception):
+def upload_image(image_path):
+ if not os.path.isfile(image_path):
+ raise ArgumentException('Invalid image path')
+ # Open the desired file
+ with open(image_path, '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,
+ 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']
+def tag_image(image, content_id=False, verbose=False, language='en'):
+ # Using the content id and the content parameter,
+ # make a GET request to the /tagging endpoint to get
+ 'content' if content_id else 'url': image,
+ tagging_response = requests.get(
+ '%s/tagging' % endpoint,
+ return tagging_response.json()
+def extract_colors(image, content_id=False):
+ 'content' if content_id else 'url': image,
+ colors_response = requests.get(
+ '%s/colors' % endpoint,
+ return colors_response.json()
+ parser = argparse.ArgumentParser(
+ description='Tags images in a folder')
+ help='The input - a folder containing images')
+ help='The output - a folder to output the results')
+ help='The language of the output tags')
+ help='Whether to use verbose mode')
+ help='Whether to generate a single output file')
+ help='Whether to do color exctraction on the images too')
+ args = parser.parse_args()
+ args = parse_arguments()
+ tag_input = args.input[0]
+ tag_output = args.output[0]
+ language = args.language
+ merged_output = args.merged_output
+ include_colors = args.include_colors
+ print('Tagging images started')
+ if os.path.isdir(tag_input):
+ images = os.listdir(tag_input)
+ images_count = len(images)
+ for iterator, image_file in enumerate(images):
+ image_path = os.path.join(tag_input, image_file)
+ print('[%s / %s] %s uploading' %
+ (iterator + 1, images_count, image_path))
+ content_id = upload_image(image_path)
+ except ArgumentException:
+ tag_result = tag_image(content_id, True, verbose, language)
+ results[image_file] = tag_result
+ colors_result = extract_colors(content_id, True)
+ results[image_file] = {
+ 'colors': colors_result
+ print('[%s / %s] %s tagged' %
+ (iterator + 1, images_count, image_path))
+ raise ArgumentException(
+ 'The input directory does not exist: %s' % tag_input)
+ if not os.path.exists(tag_output):
+ os.makedirs(tag_output)
+ elif not os.path.isdir(tag_output):
+ raise ArgumentException(
+ 'The output folder must be a directory')
+ os.path.join(tag_output, 'results.json'),
+ results, ensure_ascii=False, indent=4).encode('utf-8'))
+ for image, result in results.iteritems():
+ os.path.join(tag_output, 'result_%s.json' % image),
+ result, ensure_ascii=False, indent=4).encode('utf-8'))
+if __name__ == '__main__':