This blog post is part of series on How-Tos for those of you who are not quite experienced and need a bit of help to set up and use properly our powerful image recognition APIs.
In this one we will help you to batch process (using our Tagging or Color extraction API) a whole folder of photos, that reside on your local computer. To make that possible we’ve written a short script in the programming language Python: https://bitbucket.org/snippets/imaggateam/LL6dd
Feel free to reuse or modify it. Here’s a short explanation what it does. The script requires the Python package, which you can install using this guide.
It uses requests’ HTTPBasicAuth to initialize a Basic authentication used in Imagga’s API from a given API_KEY and API_SECRET which you have to manually set in the first lines of the script.
There are three main functions in the script – upload_image, tag_image, extract_colors.
-
- upload_image(image_path) – uploads your file to our API using the content endpoint, the argument image_path is the path to the file in your local file system. The function returns the content id associated with the image.
- tag_image(image, content_id=False, verbose=False, language=’en’) – the function tags a given image using Imagga’s Tagging API. You can provide an image url or a content id (from upload_image) to the ‘image’ argument but you will also have to set content_id=True. By setting the verbose argument to True, the returned tags will also contain their origin (whether it is coming from machine learning recognition or from additional analysis). The last parameter is ‘language’ if you want your output tags to be translated in one of Imagga’s supported 50 (+1) languages. You can find the supported languages from here – http://docs.imagga.com/#auto-tagging
- extract_colors(image, content_id=False) – using this function you can extract colors from your image using our Color Extraction API. Just like the tag_image function, you can provide an image URL or a content id (by also setting content_id argument to True).
Script usage:
Note: You need to install the Python package requests in order to use the script. You can find installation notes here.
You have to manually set the API_KEY and API_SECRET variables found in the first lines of the script by replacing YOUR_API_KEY and YOUR_API_SECRET with your API key and secret.
Usage (in your terminal or CMD):
python tag_images.py <input_folder> <output_folder> –language=<language> –verbose=<verbose> –merged-output=<merged_output> –include-colors=<include_colors>
The script has two required – <input_folder>, <output_folder> and four optional arguments – <language>, <verbose>, <merged_output>, <include_colors>.
- <input_folder> – required, the input folder containing the images you would like to tag.
- <output_folder> – required, the output folder where the tagging JSON response will be saved.
- <language> – optional, default: en, the output tags will be translated in the given language (a list of supported languages can be found here: http://docs.imagga.com/#auto-tagging)
- <verbose> – optional, default: False, if True the output tags will contain an origin key (whether it is coming from machine learning recognition or from additional analysis)
- <include_colors> – optional, default: False, if True the output will also contain color extraction results for each image.
- <merged_output> – optional, default: False, if True the output will be merged in a JSON single file, otherwise – separate JSON files for each image.