本文介绍了获得图像的主色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得图片的主色为rgb或hexcode?

我找到一个脚本,但它不允许仅显示图片网址路径。

解决方案

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

  from colorthief import ColorThief 
import urllib
import os
def dominant_color_from_url(url,tmp_file ='tmp.jpg'):
'''Downloads ths图像文件并分析主色''
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


How can I get the dominant color of an image as rgb or hexcode?

I found a script called Color Thief but it does not allow image URLs only paths.

解决方案

use urllib to download the image first, then delete the unnecessary file:

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

这篇关于获得图像的主色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 04:16