Python 获取图像的主色

Python 获取图像的主色,python,image,colors,python-imaging-library,Python,Image,Colors,Python Imaging Library,如何获得图像的主色(rgb或hexcode) 我发现一个名为的脚本,但它不允许仅使用图像URL路径。首先使用urllib下载图像,然后删除不必要的文件: from colorthief import ColorThief import urllib import os def dominant_color_from_url(url,tmp_file='tmp.jpg'): '''Downloads ths image file and analyzes the dominant colo

如何获得图像的主色(rgb或hexcode)

我发现一个名为的脚本,但它不允许仅使用图像URL路径。

首先使用urllib下载图像,然后删除不必要的文件:

from colorthief import ColorThief
import urllib
import os
def dominant_color_from_url(url,tmp_file='tmp.jpg'):
    '''Downloads ths image file and analyzes the dominant color'''
    urllib.urlretrieve(url, tmp_file)
    color_thief = ColorThief(tmp_file)
    dominant_color = color_thief.get_color(quality=1)
    os.remove(tmp_file)
    return dominant_color

如果不想下载不必要的文件,请按以下方式进行:

# -*- coding: utf-8 -*-

import sys

if sys.version_info < (3, 0):
    from urllib2 import urlopen
else:
    from urllib.request import urlopen

import io

from colorthief import ColorThief


fd = urlopen('http://lokeshdhakar.com/projects/color-thief/img/photo1.jpg')
f = io.BytesIO(fd.read())
color_thief = ColorThief(f)
print(color_thief.get_color(quality=1))
print(color_thief.get_palette(quality=1))

我假设您使用的是javascript?Color小偷使用的是jQuery,但如果有其他解决方案PHP或其他我想使用的解决方案。对于PHP,请尝试以下方法: