Python code to deal with REST API

Here we are taking the MulticolorEngine API as an example.

Making an API call by uploading images using Python:

The image file is sent to the MulticolorEngine API, but only one may be specified at a time and get the dominant colors as response. For each color, a similarity rank, a weight factor and a color class are returned along with the RGB values.

We can make the API call either by uploading the image files or by sending the image URLs. 

i) Python code using the image files:

import requests

from requests.auth import HTTPBasicAuth


api_url = 'https://multicolorengine.tineye.com/sandbox/rest/extract_image_colors/'

user = 'username’

password = 'password'

auth = HTTPBasicAuth(user, password)

files = {

    'images[0]': open('image1.jpg', 'rb')

}


response = requests.post(api_url, auth=auth, files=files)

json_data = response.json()

print(json_data)


images[n]: The image file from which to extract colors. Either this or urls is required, but only one may be specified at a time. This parameter can be included multiple times to specify multiple values, with n starting at 0 and increasing for each additional value.

Image limitations:

Image size: For optimal performance, uploaded images (those given by images[n] parameters) should be 600px in size in the smallest dimension. For example, 1200x800 pixels is larger than required and it will take longer to transfer this file to your MulticolorEngine server. It would be faster to resize this image to be 900x600 and then send it. Smaller images may work, and need not be scaled up.

Image format: Accepted formats are JPEG, PNG, WebP, GIF, BMP and TIFF files. Animated images are not supported.


ii).To send URL of an image file instead of the image file:

import requests

from requests.auth import HTTPBasicAuth


api_url = 'https://multicolorengine.tineye.com/sandbox/rest/extract_image_colors/'

user = 'username’

password = 'password'

auth = HTTPBasicAuth(user, password)

data={'limit': 2, 'urls[0]': 'https://content.website.com/images/image1.jpg'}


response = requests.post(api_url, auth=auth, data=data)

json_data = response.json()

print(json_data)


urls[n]: The publicly-accessible URL where the API can find the image from which to extract colors. Either this or images is required, but only one may be specified at a time. This parameter can be included multiple times to specify multiple values, with n starting at 0 and increasing for each additional value.

limit: For extracting only 2 colors from Image, set limit : 2

The maximum number of colors to be extracted when processing multiple image files, defaults to 32.


Sample output:

{

    "status": "ok",

    "error": [],

    "method": "extract_image_colors",

    "result": [

        {

            "color": [

                194,

                66,

                28

            ],

            "rank": 1,

            "class": "orange-dark,red",

            "weight": 76.37

        },

        {

            "color": [

                141,

                125,

                83

            ],

            "rank": 2,

            "class": "brown-light",

            "weight": 23.63

        }

    ]

}


Response Code: If we get the response status code as 401, then it is a failure/error. Response code 200 or 201 is for success.