2024-10-06 11:43:19 发布
网友
如何获得图像的主色为rgb或hexcode?在
我发现了一个名为Color Thief的脚本,但它不允许只使用图像url的路径。在
使用urllib先下载图像,然后删除不必要的文件:
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))
使用
urllib
先下载图像,然后删除不必要的文件:如果您不想下载不必要的文件,请按以下方式进行:
相关问题 更多 >
编程相关推荐