1

如何将图像的主色设为 rgb 或 hexcode?

我找到了一个名为Color Thief的脚本,但它不允许图像 URL 只有路径。

4

2 回答 2

0

如果您不想下载不必要的文件,请这样做:

# -*- 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))
于 2019-04-26T15:05:26.017 回答
0

用于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
于 2016-08-21T19:46:17.483 回答