python 处理图像(绿色亮度)

# coding=utf-8
import Image
import os
import csv
import numpy as np
# open
img_path = r"G:\kangyufeng\pics"



def myimage(img_path):
    try:
        img_list = os.listdir(img_path)
    except:
        print 'not a document'
        return
    Bigm = 0.01
    for img in img_list:
        if "tif" in img:
            print img
            im = Image.open(img_path+'\\'+img)
            #im.save("%s%s"%(img.split(".")[-2],'.jpeg'))

            w,l=im.size
            source = im.split()

            R,G,B = 0,1,2
            S = 0
            
            count = 1
            mtr = np.array(source[G])
            mtr.transpose()
            
            mtr = mtr[l/2-300:l/2+300]

            mtr = mtr.reshape(1,w*600)
            print len(mtr[0])
            mmean = mtr.mean()
            
            print mmean
            for i in mtr[0]:
                if i > mmean/10:
                    S += i
                    count += 1
            m = S/count
            if Bigm < m:
                Bigm = m
            print m,count
            
            with open("%s%s%s%s" % (img_path,'\\',img_path.split('\\')[-1],'_results.csv'),'ab') as f: #如果不使用‘ab'方式只用’a'会产生多余空行
                f = csv.writer(f,dialect='excel')
                f.writerow([img,str(m),str(float(m)/Bigm*float(img_path.split('_')[-1]))+'Mmol/L'])
        else:
            if 'csv' not in img:
                myimage(img_path+'\\'+img)


myimage(img_path)

python 处理图像(绿色亮度)

posted @ 2015-03-21 17:26  程序员阿力  阅读(1069)  评论(0编辑  收藏  举报