
import colorsys
def get_dominant_color(image):
#颜色模式转换,以便输出rgb颜色值
image = image.convert('RGBA')
#生成缩略图,减少计算量,减小cpu压力
image.thumbnail((200, 200))
max_score = None
dominant_color = None
for count, (r, g, b, a) in image.getcolors(image.size[0] 孙迅* image.size[1]):
# 跳过纯黑色
if a == 0:
梁隐 continue
saturation = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)[1]
y = min(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235)
y = (y - 16.0) / (235 - 16)
# 忽略高亮色
if y > 0.9:
continue
# Calculate the score, preferring highly saturated colors.
# Add 0.1 to the saturation so we don't completely ignore grayscale
# colors by multiplying the count by zero, but still give them a low
# weight.
score = (saturation 橡凯厅+ 0.1) * count
if score > max_score:
max_score = score
dominant_color = (r, g, b)
return dominant_color
if __name__=="__main__":
from PIL import Image
import os
path = r'.\\pics\\'
fp = open('file_color.txt','w')
for filename in os.listdir(path):
print path+filename
try:
color = get_dominant_color(Image.open(path+filename))
fp.write('The color of '+filename+' is '+str(color)+'\n')
except:
print "This file format is not support"
fp.close()
pics文件夹和python程序在一个目录下,产生的文件名file_color.txt也在这个目录下。
看看能否帮到你
//转自某某大侠的(名字忘了)此程序用于颜色模块TCS230D的51驱虚桐动(2010.08.26大侠)//鉴于你应该知道TCS230D芯片的资料,芯片引脚功能我就不团空说了
#include<reg52.h>
unsigned char tmp
/*******************************************
定义颜色识别模块引脚
*******************************************/
sbit 差或坦 clr_S0=P3^0
sbit clr_S1=P3^1
//设置p3.0脚3.1脚分别接到tcs230d芯片的s0和s1脚
sbit clr_OE=P3^2
sbit clr_OUT=P3^5
//设置p3.2脚3.5脚分别接到tcs230d芯片的OE和OUT脚
sbit clr_S2=P3^4
sbit clr_S3=P3^3
//设置p3.4脚3.3脚分别接到tCS230D芯片的S2和S3脚
/*******************************************/
/**********
串口初始化
***********************/
/*******************************************/
void init_ser()//初始化串口
{
TMOD=0x20
TH1=0xfd
TL1=0xfd
SCON=0x50
PCON&=0xef
TR1=1
IE=0x00
}
void ser_sendchar(unsigned char a)//串口发送一个字节
{
SBUF=a
while(!TI)
TI=0
}
/*******************************************/
/********
颜色识别模块配置
*******************/
/*******************************************/
void init_clr()
{
clr_OE=1
}
unsigned int clr_get(unsigned char a)
{
unsigned int time
TMOD=0x61//设置定时器0工作方式为16位
TH0=TL0=0x00//设置定时器0从0计时
TH1=TL1=0x00
clr_S0=1
clr_S1=1
clr_S2=a&0x02
clr_S3=a&0x01
clr_OE=0
TR1=TR0=1
while(TL1<250)
TR1=TR0=0
clr_OE=1
time=TH0*256+TL0
return(time)
}
/*******************************************/
/*******************************************/
/*******************************************/
void main()
{
unsigned int a
init_ser()
init_clr()
while(1)
{
if(RI)
{
RI=0
tmp=SBUF
a=clr_get(tmp)
init_ser()
ser_sendchar(a&0x00ff)
ser_sendchar(a/256)
}
}
}
以上就是整个代码部分,希望有帮助
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)