Snippets
Revised by
Georgi Kostadinov
f8246a8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | import os import requests from requests.auth import HTTPBasicAuth ### # API Credentials API_KEY = os.environ.get('IMAGGA_API_KEY', 'YOUR_API_KEY') # Set API key here API_SECRET = os.environ.get('IMAGGA_API_SECRET', 'YOUR_API_SECRET') # Set API secret here ### ENDPOINT = 'https://api.imagga.com/v1' FILE_TYPES = ['png', 'jpg', 'jpeg', 'gif'] class ArgumentException(Exception): pass if API_KEY == 'YOUR_API_KEY' or \ API_SECRET == 'YOUR_API_SECRET': raise ArgumentException('You haven\'t set your API credentials. ' 'Edit the script and set them.') auth = HTTPBasicAuth(API_KEY, API_SECRET) 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, 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'] return content_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 # image tags tagging_query = { 'content' if content_id else 'url': image, 'verbose': verbose, 'language': language } tagging_response = requests.get( '%s/tagging' % ENDPOINT, auth=auth, params=tagging_query) return tagging_response.json() def extract_colors(image, content_id=False): colors_query = { 'content' if content_id else 'url': image, } colors_response = requests.get( '%s/colors' % ENDPOINT, auth=auth, params=colors_query) return colors_response.json() def parse_arguments(): import argparse parser = argparse.ArgumentParser( description='Tags images in a folder') parser.add_argument( 'input', metavar='<input>', type=str, nargs=1, help='The input - a folder containing images') parser.add_argument( 'output', metavar='<output>', type=str, nargs=1, help='The output - a folder to output the results') parser.add_argument( '--language', type=str, default='en', help='The language of the output tags') parser.add_argument( '--verbose', type=int, default=0, help='Whether to use verbose mode') parser.add_argument( '--merged-output', type=int, default=0, help='Whether to generate a single output file') parser.add_argument( '--include-colors', type=int, default=0, help='Whether to do color exctraction on the images too') args = parser.parse_args() return args def main(): import json args = parse_arguments() tag_input = args.input[0] tag_output = args.output[0] language = args.language verbose = args.verbose merged_output = args.merged_output include_colors = args.include_colors print('Tagging images started') results = {} if os.path.isdir(tag_input): images = [filename for filename in os.listdir(tag_input) if os.path.isfile(os.path.join(tag_input, filename)) and filename.split('.')[-1].lower() in FILE_TYPES] 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)) try: content_id = upload_image(image_path) except IndexError: continue except KeyError: continue except ArgumentException: continue tag_result = tag_image(content_id, True, verbose, language) if not include_colors: results[image_file] = tag_result else: colors_result = extract_colors(content_id, True) results[image_file] = { 'tagging': tag_result, 'colors': colors_result } print('[%s / %s] %s tagged' % (iterator + 1, images_count, image_path)) else: 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') if merged_output: with open( os.path.join(tag_output, 'results.json'), 'w') as results_file: results_file.write( json.dumps( results, ensure_ascii=False, indent=4).encode('utf-8')) else: for image, result in results.iteritems(): with open( os.path.join(tag_output, 'result_%s.json' % image), 'w') as results_file: results_file.write( json.dumps( result, ensure_ascii=False, indent=4).encode('utf-8')) print('Done') if __name__ == '__main__': main() |
You can clone a snippet to your computer for local editing. Learn more.