#!/usr/bin/python3 import getopt import sys import io import json import colorsys from urllib.request import urlopen from colorthief import ColorThief ImageWebAddress = '' try: Options, args = getopt.getopt(sys.argv[1:], 'i:', ['image=']) except getopt.GetoptError: print('DominantImageColor.py -i ') sys.exit(2) for Option, Argument in Options: if Option in ('-i', '--image'): ImageWebAddress = Argument if ImageWebAddress.strip() == '': print('Image web address parameter is mandatory') sys.exit(2) OriginalImage = urlopen(ImageWebAddress) ImageBinary = io.BytesIO(OriginalImage.read()) ImageColorThief = ColorThief(ImageBinary) DominantColor = ImageColorThief.get_color(quality=5) DominantColorHSV = colorsys.rgb_to_hsv(int(DominantColor[0]) / 255, int(DominantColor[1]) / 255, int(DominantColor[2]) / 255) JSONDominantColor = { 'hue' : int(DominantColorHSV[0] * 360), 'saturation' : int(DominantColorHSV[1] * 100), 'brightness' : int(DominantColorHSV[2] * 100) } FormatedJSONDominantColor = json.dumps(JSONDominantColor) sys.stdout.write(FormatedJSONDominantColor)