我正在写一个程序来分析图像(绘画、素描等)。dominant_color
函数应该返回一个元组列表[(count, (r#, b#, g#)), ...]
,按count
降序排序。你知道吗
下面是脚本,但我不确定如何正确地给filename
一个有效值。你知道吗
我试过filename = 'path to file'
,但是得到了syntax error
,除非代码是在PyCharm中运行的。你知道吗
def dominant_color(filename):
#Resizing parameters
width, height = 150,150
image = Image.open(filename)
image = image.resize((width, height), resample = 0)
#Convert image to RGB
im_rgb = im.convert('RGB')
#Get colors from image object
pixels = image.getcolors(width * height)
#Sort them by count number(first element of tuple)
sorted_pixels = sorted(pixels, key=lambda t: t[0])
return sorted_pixels
filename
的dominant_color
时,会发生syntax error
sorted_pixels
列表不会首先返回最主要的颜色。你知道吗sorted_pixels
应按降序返回。你知道吗
确保将文件路径作为原始字符串传递。 我的意思是这样写文件路径:
使用
pathlib
查找文件os
。你知道吗输出:
从命令行:
p
是如何构建的相关问题 更多 >
编程相关推荐