标签:result int def 相关 res 安装 实例 jpeg 设置
1 import numpy as np 2 import PIL.Image as image 3 from sklearn.cluster import KMeans 4 5 def loadData(filePath): 6 f = open(filePath,‘rb‘) 7 data = [] 8 img = image.open(f) 9 m,n = img.size 10 for i in range(m): 11 for j in range(n): 12 x,y,z = img.getpixel((i,j)) 13 data.append([x/256.0,y/256.0,z/256.0]) 14 f.close() 15 return np.mat(data),m,n 16 17 imgData,row,col = loadData(‘kmeans/bull.jpg‘) 18 label = KMeans(n_clusters=4).fit_predict(imgData) 19 20 label = label.reshape([row,col]) 21 pic_new = image.new("L", (row, col)) 22 for i in range(row): 23 for j in range(col): 24 pic_new.putpixel((i,j), int(256/(label[i][j]+1))) 25 pic_new.save("result-bull-4.jpg", "JPEG")
标签:result int def 相关 res 安装 实例 jpeg 设置
原文地址:https://www.cnblogs.com/nishida-rin/p/12275057.html