def tag_images_with_color_value(NUM_CLUSTERS = 4, INPUT_FOLDER = './data/covers/'): isbn = list() cover_color = list() files = os.listdir(INPUT_FOLDER) for eachFile in files: print eachFile im = Image.open(INPUT_FOLDER + eachFile) im = im.resize((50, 50)) # optional, to reduce time ar = scipy.misc.fromimage(im) shape = ar.shape print len(shape) if len(shape) == 2: ar = ar.reshape(scipy.product(shape[:1]), shape[1]) else: ar = ar.reshape(scipy.product(shape[:2]), shape[2]) # finding clusters codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) # cluster centres:\n', codes vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] colour = ''.join(chr(c) for c in peak).encode('hex') isbn.append(eachFile[:-4]) cover_color.append(colour) result = zip(isbn, cover_color) return result
def testReductionSum2(self): dds = mango.ones(shape=self.shape, dtype="uint16", halo=2) dds.setBorderToValue(100) centreIdx = tuple(sp.array(dds.subd.shape) // 2) if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx] = 2 expectSum = sp.product(dds.shape) + 2 * 2 - 1 s = mango.sum2(dds, dtype="uint64") self.assertEqual(expectSum, s) dds = mango.ones(shape=self.shape, mtype="tomo", halo=2) dds.setBorderToValue(100) centreIdx = tuple(sp.array(dds.subd.shape) // 2) if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx] = 9 expectSum = sp.product(dds.shape) + 9 * 9 - 1 s = mango.sum2(dds, dtype="uint64") self.assertEqual(expectSum, s) dds = mango.ones(shape=self.shape, mtype="tomo", halo=2) dds.setBorderToValue(0) centreIdx = tuple(sp.array(dds.subd.shape) // 2) if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx] = 9 if ((dds.mpi.comm == None) or (dds.mpi.comm.Get_rank() == 0)): dds[centreIdx[0], centreIdx[1], centreIdx[2] + 1] = dds.mtype.maskValue() expectSum = sp.product(dds.shape) + 9 * 9 - 1 - 1 s = mango.sum2(dds, dtype="uint64") self.assertEqual(expectSum, s)
def getDominantColors(npRGB, numColors): # print(npRGB.shape) height = npRGB.shape[0] width = npRGB.shape[1] # FIXME: extracting colors should ne in a utility function npOrig = npRGB # image in original size if width * height > 150 * 150: npRGB = UtilCanvas.resize2017(npRGB, '150x150') # to reduce time shape = npRGB.shape npRGB = npRGB.reshape(scipy.product(shape[:2]), shape[2]) shapeOrig = npOrig.shape npOrig = npOrig.reshape(scipy.product(shapeOrig[:2]), shapeOrig[2]) #print('finding clusters') colors, distances = scipy.cluster.vq.kmeans(npRGB, numColors) #print('cluster centres:\n', colors, distances) # ---- sort colors by count # vecs, distances = scipy.cluster.vq.vq(npOrig, colors) # assign codes # counts = [] # for i, color in enumerate(colors): # count = len(scipy.where(vecs == i)[0]) # counts.append(count) # print(counts) # counts, colors = zip( *sorted( zip(counts, colors), reverse=True ) ) # print(counts) return colors
def get_predominant_colour(album: Album) -> str: """ Gets the most predominant colour in an image. :param album: The album associated with the image :return: A hex code (including starting hash) """ num_clusters = 5 image = get_raw_image(album, width=150) ar = np.asarray(image) shape = ar.shape if len(shape) > 2: ar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float) else: # TODO Actually figure out what's going wrong here ar = ar.reshape(scipy.product(shape[:1]), shape[1]).astype(float) codes, dist = scipy.cluster.vq.kmeans(ar, num_clusters) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] colour = '#' for c in peak: char = str(hex(int(c)))[2:] if len(char) == 1: char = '0' + char colour += char return colour
def cluster_multiple_images(images, k_clusters=10): ''' Clusters an image into k cluster points. Then, converts each color point from RGB to LAB color format. Args: images: An array of open PIL image k_clusters: Number of clusters to produce. Defaulted to 10. Returns: cluster_list: A list containing elements in the following format: [(name), (array of colors)] Replaced vq with sklearn ''' fullarr = scipy.misc.fromimage(images[0]) shape = fullarr.shape fullarr = fullarr.reshape(scipy.product(shape[:2]), shape[2]) # print('fullarr', fullarr.shape) for im in images[1:]: arr = scipy.misc.fromimage(im) shape = arr.shape if len(shape) > 2: arr = arr.reshape(scipy.product(shape[:2]), shape[2]) # print(arr.shape) fullarr = numpy.concatenate((fullarr, arr), axis=0) # print('fullarr',fullarr.shape) else: print('Invalid image') rgblist = [sRGBColor(z[0] / 255, z[1] / 255, z[2] / 255) for z in fullarr] lablist = [convert_color(x, LabColor) for x in rgblist] lablist = numpy.array([[x.lab_l, x.lab_a, x.lab_b] for x in lablist]) k_means_ex = KMeans(n_clusters=k_clusters) x = k_means_ex.fit_predict(lablist) codes = k_means_ex.cluster_centers_ labels = k_means_ex.labels_ return codes, labels
def prblm_8(): """Find the largest product of 13 adjacent digits in the following series""" from scipy import product series = """73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450""".replace('\n','') serilist = [int(x) for x in series] heighestprod = 0 for ii in range(0, len(serilist) - 13): if product(serilist[ii:ii + 13]) > heighestprod: heighestprod = product(serilist[ii:ii+13]) heighestset = serilist[ii:ii+13] return heighestprod, heighestset
def test(): intlist = load_test() triads = get_groups(intlist, cachefile=TESTCACHE) x = itergroups(triads).next() print 'group 1: {}'.format(x) print 'QE : {}'.format(scipy.product(list(x[0]))) quads = get_groups(intlist, num=4, cachefile=TESTCACHE) x = itergroups(quads, num=4).next() print 'group 1: {}'.format(x) print 'QE : {}'.format(scipy.product(list(x[0])))
self.mapping[indexes[i]] = finalbeta[i] return self.mapping def stats(self, startdate, enddate, mktbasket, output = False): """ Calculates statistics for a fund over a period. Parameters ---------- startdate : datetime beginning of statistic period enddate : datetime end of statistic period mktbasket : dict dictionary of market streams output : bool if True, output results to db Returns ------- stats : dict dictionary of statistics """ inputmatrix, fundreturns, indexes, daterange = self.align(startdate, enddate, mktbasket) if self.mapping and not(inputmatrix is None): weights = scipy.array([self.mapping[mykey] if mykey in self.mapping else 0.0 for mykey in mktbasket.keys()]) projected = scipy.dot(inputmatrix,weights.reshape(len(indexes),1)).flatten() actual = fundreturns.flatten() diff = actual-projected outdata = { 'TE' : scipy.std(diff)*100.0*100.0, 'BETA' : scipy.cov(projected,actual)[1,0]/scipy.var(projected), 'ALPHA' : (scipy.product(diff+1.0))**(1.0/diff.size)-1.0, 'VOL' : scipy.std(actual)*scipy.sqrt(252.0), 'PROJ' : scipy.product(1.0+projected)-1.0, 'ACT' : scipy.product(1.0+actual)-1.0, 'R2' : 0.0 if scipy.all(actual==0.0) else scipy.corrcoef(projected,actual)[1,0]**2.0, 'AV' : self.av(startdate), 'DELTA' : self.deltaestimate(startdate) } outdata['DIFF'] = outdata['ACT']-outdata['PROJ'] outdata['PL'] = outdata['DELTA']*outdata['DIFF']*100.0 if output: cnxn = pyodbc.connect(ORACLESTRING) cursor = cnxn.cursor() sql = 'INSERT INTO FUNDOUTPUT VALUES ({0!s},{1!s},{2!s},{3!s},{4!s},{5!s},{6},{7},{8!s},{9!s},{10!s},{11!s},{12!s},{13!s});' sql = sql.format(self.fundcode,outdata['PROJ'],outdata['ACT'],outdata['DIFF'], outdata['DELTA'],outdata['PL'],oracledatebuilder(startdate), oracledatebuilder(enddate),outdata['TE'],outdata['R2'],outdata['BETA'], outdata['ALPHA'],outdata['VOL'],outdata['AV']) cursor.execute(sql) cnxn.commit() cnxn.close()
def stats(self, startdate, enddate, mktbasket, avdate, output=False, mappingoverride=None): """ Calculates statistics for a fund over a period. Parameters ---------- startdate : datetime beginning of statistic period enddate : datetime end of statistic period mktbasket : dict dictionary of market streams output : bool if True, output results to db mappingoverride : None or mapping dictionary whether to override the db mapping Returns ------- stats : dict dictionary of statistics """ actualstream, projstream = self.project(mktbasket, mappingoverride) if actualstream[startdate:enddate] is None: return None if projstream[startdate:enddate] is None: return None actual = actualstream[startdate:enddate].returns projected = projstream[startdate:enddate].returns diff = actual - projected outdata = { 'TE' : scipy.std(diff) * 100.0 * 100.0, 'BETA' : scipy.cov(projected, actual, bias=1)[1, 0] / scipy.var(projected), 'ALPHA' : (scipy.product(diff + 1.0)) ** (1.0 / diff.size) - 1.0, 'VOL' : scipy.std(actual) * scipy.sqrt(252.0), 'PROJ' : scipy.product(1.0 + projected) - 1.0, 'ACT' : scipy.product(1.0 + actual) - 1.0, 'R2' : 0.0 if scipy.all(actual == 0.0) else scipy.corrcoef(projected, actual)[1, 0] ** 2.0, 'AV' : self.av(avdate), 'DELTA' : self.deltaestimate(avdate) } outdata['DIFF'] = outdata['ACT'] - outdata['PROJ'] outdata['PL'] = outdata['DELTA'] * outdata['DIFF'] * 100.0 if output: cnxn = pyodbc.connect(ORACLESTRING) cursor = cnxn.cursor() sql = 'INSERT INTO FUNDOUTPUT VALUES ({0!s},{1!s},{2!s},{3!s},{4!s},{5!s},{6},{7},{8!s},{9!s},{10!s},{11!s},{12!s},{13!s});' sql = sql.format(self.fundcode, outdata['PROJ'], outdata['ACT'], outdata['DIFF'], outdata['DELTA'], outdata['PL'], oracledatebuilder(startdate), oracledatebuilder(enddate), outdata['TE'], outdata['R2'], outdata['BETA'], outdata['ALPHA'], outdata['VOL'], outdata['AV']) cursor.execute(sql) cnxn.commit() cnxn.close() return outdata
def get_dominant_color(pic_name): """ This function gets the dominant colour in an image given its path. Parameters ---------- pic_name : str The picture path. Returns ------- list The peak colour and the dominant colour. """ NUM_CLUSTERS = 5 im = Image.open(pic_name) im = im.resize((150, 150)) # optional, to reduce time ar = np.asarray(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float) codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) # finding clusters vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] for color in peak: color = int(color) colour = binascii.hexlify(bytearray(int(c) for c in peak)).decode('ascii') return peak, colour
def getDominantColor(img_url): if r.exists(img_url): cache_result = r.hmget(img_url, ['r', 'g', 'b']) return cache_result NUM_CLUSTERS = 5 im = Image.open(StringIO.StringIO(urllib2.urlopen(img_url).read())) img_arr = scipy.misc.fromimage(im) img_shape = img_arr.shape if len(img_shape) > 2: img_arr = img_arr.reshape(scipy.product(img_shape[:2]), img_shape[2]) codes, _ = scipy.cluster.vq.kmeans(img_arr, NUM_CLUSTERS) original_codes = codes for low, hi in [(60, 200), (35, 230), (10, 250)]: codes = scipy.array([code for code in codes if not (all([c < low for c in code]) or all([c > hi for c in code]))]) if not len(codes): codes = original_codes else: break vecs, _ = scipy.cluster.vq.vq(img_arr, codes) counts, bins = scipy.histogram(vecs, len(codes)) index_max = scipy.argmax(counts) peak = codes[index_max] color = [int(c) for c in peak[:3]] r.hmset(img_url, {'r':color[0], 'g':color[1], 'b':color[2]}) #r.expire(img_url, 86400) return color
def filter_white_background(dataset, n=50, NUM_CLUSTERS=5): """Filter dataset for those images which are predominately white.""" path = glob('./%s/*' % (dataset)) to_keep = [] for i in range(len(path)): img = scipy.misc.imread(path[i], mode='RGB').astype(np.float) shape = img.shape img = img.reshape(scipy.product(shape[:2]), shape[2]).astype(float) codes, dist = scipy.cluster.vq.kmeans(img, NUM_CLUSTERS) vecs, dist = scipy.cluster.vq.vq(img, codes) # Assign codes counts, bins = scipy.histogram(vecs, len(codes)) # Count occurrences index_max = scipy.argmax(counts) # Find most frequent peak = codes[index_max] color = [int(c) for c in peak] if color[0] + color[1] + color[2] >= 750: to_keep.append(path[i]) print('keep image {}: most frequent is {}'.format(i, color)) with open(dataset + '_to_keep.txt', 'w') as f: for item in to_keep: f.write('%s\n' % item)
def get_predominant_color(image): NUM_CLUSTERS = 5 im = image.convert('RGB') # Convert to numpy array ar = scipy.misc.fromimage(im) # Get dimensions shape = ar.shape # Convert to bidimensional array of width x height rows and 3 columns (RGB) ar = ar.reshape(scipy.product(shape[:2]), shape[2]) # Find cluster centers and their distortions # codes contains the RGB value of the centers codes, dist = scipy.cluster.vq.kmeans(ar.astype(float), NUM_CLUSTERS) # Maps all the pixels in the image to their respective centers vecs, dist = scipy.cluster.vq.vq(ar, codes) # Counts the occurances of each color (NUM_CLUSTER different colors after the mapping) counts, bins = scipy.histogram(vecs, len(codes)) # Find most frequent color index_max = scipy.argmax(counts) peak = codes[index_max] return peak.astype(int)
def get_dominant_colour(img): # https://stackoverflow.com/questions/3241929/python-find-dominant-most-common-color-in-an-image NUM_CLUSTERS = 5 ar = np.asarray(img) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float) # print ('finding clusters') codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) # print ('cluster centres:\n', codes) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] peak_int = [int(c) for c in peak] colour = rgb2hex(peak_int[0], peak_int[1], peak_int[2]) # print ('Processed image, most frequent colour is %s (#%s)' % (peak_int, colour)) # lets show the most frequent colour #blank = np.zeros((100,100,3), np.uint8) #blank[:] = (int(peak_int[0]), int(peak_int[1]), int(peak_int[2])) # cv2.imshow("orig", img) #cv2.imshow("colour", blank) #cv2.waitKey(0) #cv2.destroyAllWindows() return (peak_int[0], peak_int[1], peak_int[2])
def cluster_colors(image_url, num_clusters=5): """ Return the most clustered colors of an image. Use scipy's k-means clustering algorithm. """ print 'Reading image...' response = requests.get(image_url) im = Image.open(StringIO(response.content)) im = im.resize((150, 150)) # optional, to reduce time ar = scipy.misc.fromimage(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]) ar = ar.astype(float) print 'Finding clusters...' # k-means clustering codes, dist = scipy.cluster.vq.kmeans(ar, num_clusters) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences sorted_index = sorted(range(len(counts)), key=lambda index: counts[index], reverse=True) most_common_colors = [] for index in sorted_index: peak = codes[index] peak = peak.astype(int) colour = ''.join(format(c, '02x') for c in peak) most_common_colors.append('#' + colour) return most_common_colors
def getColorCounts(image, focusCenter=False, darkThreshold=None, lightThreshold=None): ar = np.asarray(image) if focusCenter: y, x, z = ar.shape centerWidth = int(x * CENTER_WIDTH_PERC) centerHeight = int(y * CENTER_HEIGHT_PERC) startx = x // 2 - (centerWidth // 2) starty = y // 2 - (centerHeight // 2) ar = ar[starty:starty + centerHeight, startx:startx + centerWidth] shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float) codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) print(len(codes)) if darkThreshold != None: codes = excludeDarks(codes, darkThreshold) if lightThreshold != None: codes = excludeLights(codes, lightThreshold) print(len(codes)) print(codes) if (len(codes) == 0): return [], [] else: vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences return codes, counts
def maskLowStddVoxels(self, dds, nMeanDds, nStddDds): unique = np.unique(sp.where(nStddDds.subd.asarray() <= 1.0/3, dds.subd.asarray(), dds.mtype.maskValue())) unique = unique[sp.where(unique != dds.mtype.maskValue())] if (dds.mpi.comm != None): unique = dds.mpi.comm.allreduce(unique.tolist(), op=mpi.SUM) unique = np.unique(unique) rootLogger.info("Unique constant stdd values = %s" % (unique,)) rootLogger.info("Creating mask from unique constant values...") mskDds = mango.zeros_like(dds, mtype="segmented") for uVal in unique: mskDds.asarray()[...] = sp.where(dds.asarray() == uVal, 1, mskDds.asarray()) rootLogger.info("Done creating mask from unique constant values.") rootLogger.info("Labeling connected constant zero-stdd regions...") mskDds.updateHaloRegions() mskDds.mirrorOuterLayersToBorder(False) self.writeIntermediateDds("_000ZeroStddForLabeling", mskDds) lblDds = mango.image.label(mskDds, 1) rootLogger.info("Done labeling connected constant stdd regions.") self.writeIntermediateDds("_000ZeroStdd", lblDds) countThresh = 0.01 * sp.product(lblDds.shape) rootLogger.info("Eliminating large clusters...") lblDds = mango.image.eliminate_labels_by_size(lblDds, minsz=int(countThresh), val=lblDds.mtype.maskValue()) self.writeIntermediateDds("_000ZeroStddLargeEliminated", lblDds) rootLogger.info("Assigning mask values...") mskDds.subd.asarray()[...] = \ sp.where(lblDds.subd.asarray() == lblDds.mtype.maskValue(), True, False) self.writeIntermediateDds("_000ZeroStddMskd", mskDds) del lblDds for tmpDds in [dds, nMeanDds, nStddDds]: tmpDds.subd.asarray()[...] = \ sp.where(mskDds.subd.asarray(), tmpDds.mtype.maskValue(), tmpDds.subd.asarray())
def getDominantColor(imName): ''' Argumentos: imName = Nombre de la imagen que se va a analizar Retorno: Una tupla de dos elementos: peak = Codificacion similar a esto [244.00661895 5.01980305 2.19641608] (Creo que es RGB) colour = Codificacion de la forma (#b'\xc3\xb4\x05\x02') (Esto ya...) Yo no se ver los colores a ojo, maybe tu si. NO PIERDAS EL TIEMPO EN ESTA FUNCION SALVO QUE NOTES QUE EFECTIVAMENTE NO ESTA SACANDO EL COLOR DOMINANTE ''' #print('reading image') im = Image.open(imName) im = im.resize((150, 150)) # optional, to reduce time ar = np.asarray(im) im.close() shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float) #print('finding clusters') codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) #print('cluster centres:\n', codes) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] colour = ''.join(chr(int(c)) for c in peak).encode() return peak, colour
def find_dominant_colors(image): """Cluster the colors of the image in CLUSTER_NUMBER of clusters. Returns an array of dominant colors reverse sorted by cluster size. """ array = img_as_float(fromimage(image)) # Reshape from MxNx4 to Mx4 array array = array.reshape(scipy.product(array.shape[:2]), array.shape[2]) # Remove transparent pixels if any (channel 4 is alpha) if array.shape[-1] > 3: array = array[array[:, 3] == 1] # Finding centroids (centroids are colors) centroids, _ = kmeans(array, CLUSTER_NUMBER) # Allocate pixel to a centroid cluster observations, _ = vq(array, centroids) # Calculate the number of pixels in a cluster histogram, _ = scipy.histogram(observations, len(centroids)) # Sort centroids by number of pixels in their cluster sorted_centroids = sorted(zip(centroids, histogram), key=lambda x: x[1], reverse=True) sorted_colors = tuple((couple[0] for couple in sorted_centroids)) return sorted_colors
def getDomIMAGEColor(imName): # Reference: # http://stackoverflow.com/questions/3241929/ # python-find-dominant-most-common-color-in-an-image # number of k-means clusters NUM_CLUSTERS = 4 # Open target image im = imName im = im.resize((150, 150)) # optional, to reduce time ar = scipy.misc.fromimage(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]) ar = ar.astype(float) # Find clusters codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences # Find most frequent index_max = scipy.argmax(counts) peak = codes[index_max] color = ''.join(chr(int(c)) for c in peak).encode('hex') return (peak, color)
def quantize(img, NUM_CLUSTERS=5): ar = np.asarray(img) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float) codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes return vecs
def GetImage_DominantColor(Surface, Number_Clusters=5): """ Get the Dominant Color of a Image :param Surface: :param Number_Clusters: :return: """ strFormat = 'RGBA' raw_str = pygame.image.tostring(Surface, strFormat) ConvertedImage = Image.frombytes(strFormat, Surface.get_size(), raw_str) ConvertedImage = ConvertedImage.resize( (100, 100)) # optional, to reduce time ar = np.asarray(ConvertedImage) Shape = ar.Shape ar = ar.reshape(scipy.product(Shape[:2]), Shape[2]).astype(float) codes, dist = scipy.cluster.vq.kmeans(ar, Number_Clusters) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] return peak
def getMostDominantColour(self, image): """ Retourne la couleur dominante de l'image. Args: image (Image PIL) : l'image qu'on considère pour l'étude. Returns: (tuple) La couleur dominante de l'image dans le repère lab*. """ ### Définition du nombre de clusters pour les pixels. ### NUM_CLUSTERS = 5 ### On resize l'image pour que les temps de traitement soient réduits. ### ar = np.array(image) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float) ### On opère un K-mean sur les pixels de l'image. ### codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) vecs, dist = scipy.cluster.vq.vq(ar, codes) counts, bins = scipy.histogram(vecs, len(codes)) index_max = scipy.argmax(counts) ### La couleur la plus importante de l'image, déduite du décompte de l'histogramme des couleurs. ### peak = codes[index_max] ### Conversion de la couleur RGB dans l'espace lab*. ### rgb = sRGBColor(*peak) return convert_color(rgb, LabColor)
def testGaussianPValue(self): for typePair in [(None, "float32"), ("tomo", None)]: mtype = typePair[0] dtype = typePair[1] mean = 32000.0 stdd = 1000.0 noisDds = mango.data.gaussian_noise(shape=(105,223,240), mean=mean, stdd=stdd, mtype=mtype, dtype=dtype) pvalDds = \ mango.fmm.gaussian_pvalue( noisDds, mean=mean, stdd=stdd, sidedness=mango.fmm.PValueSidednessType.RIGHT_SIDEDNESS ) alpha = 0.05 count = sp.sum(sp.where(pvalDds.asarray() <= alpha, 1, 0)) if (pvalDds.mpi.comm != None): count = pvalDds.mpi.comm.allreduce(count) expCount = sp.product(noisDds.shape)*alpha count = float(count) relErr = sp.absolute(expCount-float(count))/sp.absolute(max(expCount,count)) rootLogger.info("relErr = %s" % relErr) self.assertTrue(relErr < 0.10)
def get_vector(self): self.img = Image.open(self.imagepath) self.img_array = np.asarray(self.img) self.vector = self.img_array.reshape( sp.product(self.img_array.shape[:2]), self.img_array.shape[2]).astype(float)
def find_color_clusters(image_path): FINAL_COLOURS = [] NUM_CLUSTERS = 50 im = Image.open(image_path) im = im.resize((100, 100)) ar = np.asarray(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float) codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent for code in codes: colour = binascii.hexlify(bytearray(int(c) for c in code)).decode('ascii') rgb_colour = tuple(int(colour[i:i + 2], 16) for i in (0, 2, 4)) final = get_colour_name(rgb_colour) FINAL_COLOURS.append(final[1]) counts = dict() for i in FINAL_COLOURS: counts[i] = counts.get(i, 0) + 1 return counts
def getDomIMAGEColor( imName ): # Reference: # http://stackoverflow.com/questions/3241929/ # python-find-dominant-most-common-color-in-an-image # number of k-means clusters NUM_CLUSTERS = 4 # Open target image im = imName im = im.resize((150, 150)) # optional, to reduce time ar = scipy.misc.fromimage(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]) ar = ar.astype(float) # Find clusters codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences # Find most frequent index_max = scipy.argmax(counts) peak = codes[index_max] color = ''.join(chr(int(c)) for c in peak).encode('hex') return (peak, color)
def determine_dominant_color_in_image(self, image): NUM_CLUSTERS = 5 ar = scipy.misc.fromimage(image) shape = ar.shape if len(shape) > 2: ar = ar.reshape(scipy.product(shape[:2]), shape[2]) codes, _ = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) # print "Before: %s" % codes original_codes = codes for low, hi in [(60, 200), (35, 230), (10, 250)]: codes = scipy.array([code for code in codes if not ((code[0] < low and code[1] < low and code[2] < low) or (code[0] > hi and code[1] > hi and code[2] > hi))]) if not len(codes): codes = original_codes else: break # print "After: %s" % codes vecs, _ = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences # colors = [''.join(chr(c) for c in code).encode('hex') for code in codes] # total = scipy.sum(counts) # print dict(zip(colors, [count/float(total) for count in counts])) index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] color = ''.join(chr(c) for c in peak).encode('hex') # print 'most frequent is %s (#%s)' % (peak, color) return color[:6]
def determine_dominant_color_in_image(self, image): NUM_CLUSTERS = 5 ar = scipy.misc.fromimage(image) shape = ar.shape if len(shape) > 2: ar = ar.reshape(scipy.product(shape[:2]), shape[2]) codes, _ = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) # print "Before: %s" % codes original_codes = codes for low, hi in [(60, 200), (35, 230), (10, 250)]: codes = scipy.array([ code for code in codes if not ((code[0] < low and code[1] < low and code[2] < low) or (code[0] > hi and code[1] > hi and code[2] > hi)) ]) if not len(codes): codes = original_codes else: break # print "After: %s" % codes vecs, _ = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences # colors = [''.join(chr(c) for c in code).encode('hex') for code in codes] # total = scipy.sum(counts) # print dict(zip(colors, [count/float(total) for count in counts])) index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] color = ''.join(chr(c) for c in peak).encode('hex') # print 'most frequent is %s (#%s)' % (peak, color) return color[:6]
def cluster(img1,Num_cluster): global pro img1 = cv2.GaussianBlur(img1,(7,7),0) roi = img1 img = Image.fromarray(roi) ar = scipy.misc.fromimage(img) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]) codes, _ = scipy.cluster.vq.kmeans(ar.astype(float), Num_cluster) vecs, _ = scipy.cluster.vq.vq(ar, codes) res = codes[vecs] result = res.reshape((shape)) result = result.astype(np.uint8) #cv2.imshow("result",result) counts, bins = scipy.histogram(vecs, len(codes)) idx = np.argsort(counts) peakb = np.int0(codes[idx[-2]]) peakc = np.int0(codes[idx[-3]]) #cv2.imshow("wss",result) low = np.array([limit(peakc[0]-1),limit(peakc[1]-1),limit(peakc[2]-1)], dtype = "uint8") high = np.array([limit(peakc[0]+1),limit(peakc[1]+1),limit(peakc[2]+1)], dtype = "uint8") result = cv2.inRange(result,low,high) pro = nearest(peakb[::-1]) + " " + nearest(peakc[::-1]) + " " cv2.waitKey(0) return result
def detect_colors(image): image = image.resize((RESIZE, RESIZE)) array = np.asarray(image.convert('RGB')) shape = array.shape array = array.reshape(scipy.product(shape[:2]), shape[2]).astype(float) codes, dist = cluster.vq.kmeans(array, NUM_CLUSTERS) vecs, dist = cluster.vq.vq(array, codes) counts, bins = scipy.histogram(vecs, len(codes)) total = np.sum(np.array(counts)) percentages = (counts / total) color_list = [] for code in codes: hex_color = binascii.hexlify(bytearray( int(c) for c in code )).decode('ascii') color_list.append(f"#{hex_color}") dom_code = codes[scipy.argmax(counts)] dom_hex = binascii.hexlify(bytearray( int(c) for c in dom_code )).decode('ascii') dom_color = f"#{dom_hex}" colors = color_list return list(colors), list(percentages), dom_color
def getPredominantColor(filename): im = Image.open(filename).convert('RGB') # Convert to numpy array ar = scipy.misc.fromimage(im) # Get dimensions shape = ar.shape # Convert to bidimensional array of width x height rows and 3 columns (RGB) ar = ar.reshape(scipy.product(shape[:2]), shape[2]) # Find cluster centers and their distortions # codes contains the RGB value of the centers codes, dist = scipy.cluster.vq.kmeans(ar.astype(float), NUM_CLUSTERS) # Maps all the pixels in the image to their respective centers vecs, dist = scipy.cluster.vq.vq(ar, codes) # Counts the occurances of each color (NUM_CLUSTER different colors after the mapping) counts, bins = scipy.histogram(vecs, len(codes)) # Find most frequent color index_max = scipy.argmax(counts) peak = codes[index_max] return peak.astype(int)
def find_a_dominant_color(image): # K-mean clustering to find the k most dominant color, from: # http://stackoverflow.com/questions/3241929/python-find-dominant-most-common-color-in-an-image n_clusters = 5 # Get image into a workable form im = image.copy() im = im.resize((150, 150)) # optional, to reduce time ar = scipy.misc.fromimage(im) im_shape = ar.shape ar = ar.reshape(scipy.product(im_shape[:2]), im_shape[2]) ar = np.float_(ar) # Compute clusters codes, dist = scipy.cluster.vq.kmeans(ar, n_clusters) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences # Get the indexes of the most frequent, 2nd most frequent, 3rd, ... sorted_idxs = np.argsort(counts) # Get the color peak = codes[sorted_idxs[1]] # get second most frequent color return [int(i) for i in peak.tolist() ] # list comprehension to quickly cast everything to int
def calculate_dominant_colors(image, num_clusters): ar = scipy.misc.fromimage(image) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]) codes, _ = scipy.cluster.vq.kmeans(ar.astype(float), num_clusters) vecs, _ = scipy.cluster.vq.vq(ar, codes) return ar, shape, codes, vecs
def cluster_image(image, k_clusters=5): ''' Clusters an image into k cluster points. Then, converts each color point from RGB to LAB color format. Args: image: An open PIL image k_clusters: Number of clusters to produce. Defaulted to 10. Returns: cluster_list: A list containing elements in the following format: [(name), (array of colors)] Replaced vq with sklearn ''' arr = scipy.misc.fromimage(image) shape = arr.shape if len(shape) > 2: arr = arr.reshape(scipy.product(shape[:2]), shape[2]) rgblist = [sRGBColor(z[0] / 255, z[1] / 255, z[2] / 255) for z in arr] lablist = [convert_color(x, LabColor) for x in rgblist] lablist = numpy.array([[x.lab_l, x.lab_a, x.lab_b] for x in lablist]) # codes, dist = scipy.cluster.vq.kmeans2(lablist, k_clusters, iter=20) # print('LEN clusters', len(codes)) return cluster_array(k_clusters, lablist) else: print('Invalid image') return [], []
def get_dominant_color(image_path): ''' Parse image and return dominant color in image. @param image_path: Image path to parse. @return: Return dominant color, format as hexadecimal number. ''' # print 'reading image' im = Image.open(image_path) im = im.resize((150, 150)) # optional, to reduce time ar = scipy.misc.fromimage(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]) # print 'finding clusters' NUM_CLUSTERS = 5 codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) # print 'cluster centres:\n', codes vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] colour = ''.join(chr(c) for c in peak).encode('hex') return "#%s" % (colour[0:6])
def get_main_colors(image_folder, list_images_names, Nmain=3, NUM_CLUSTERS=5): ar = [] for j in range(len(list_images_names)): im = Image.open(image_folder + list_images_names[j]) im = im.resize((150, 150)) # optional, to reduce time ar1 = scipy.misc.fromimage(im) if list_images_names[j].find(".png") >= 0: ar1 = ar1[:, :, 0:3] shape = ar1.shape print(shape) ar1 = ar1.reshape(scipy.product(shape[:2]), shape[2]) ar.append(ar1) ar = np.vstack(ar) codes, dist = scipy.cluster.vq.kmeans(ar.astype(float), NUM_CLUSTERS) vecs, dist = scipy.cluster.vq.vq(ar.astype(float), codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences MainRepeated = counts.argsort()[-Nmain:][::-1] perc = 100 * counts[MainRepeated] / len(ar) colors_rep = [] for j in range(Nmain): colors_rep.append(codes[MainRepeated[j]].astype(int)) print(colors_rep, perc) return colors_rep, perc
def test_cluster_image(image, k_clusters=10): img = Image.open(image) img = img.resize((150, 150)) # optional, to reduce time arr = scipy.misc.fromimage(img) shape = arr.shape # print(shape, len(shape)) if len(shape) > 2: arr = arr.reshape(scipy.product(shape[:2]), shape[2]) rgblist = [sRGBColor(z[0] / 255, z[1] / 255, z[2] / 255) for z in arr] lablist = [convert_color(x, LabColor) for x in rgblist] lablist = numpy.array([[x.lab_l, x.lab_a, x.lab_b] for x in lablist]) # print('len(lablist)',len(lablist)) # print('finding clusters') # for i in range(0,5): #try 5 times # #http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.cluster.vq.kmeans.html # codes, dist = scipy.cluster.vq.kmeans(lablist, k_clusters) # print('LEN clusters', len(codes)) # #print('codes', codes) # #print('dist', dist) # render_palette(codes, 'DS_clusters_slice'+str(i), 'LAB') # #break print('Trying Sci kit') # http://scipy-user.10969.n7.nabble.com/kmeans2-question-issue-td1883.html for i in range(0, 5): # try 5 times k_means_ex = KMeans(n_clusters=10, n_init=20) x = k_means_ex.fit_predict(lablist) codes = k_means_ex.cluster_centers_ # print(x) print(len(codes)) # , codes) render_palette(codes, 'DS_clusters_SCILEARN_slice' + str(i), 'LAB') else: print('invalid image')
def get_color_clusters(img, idx, save_images=False, clusters=3): ar = np.asarray(img) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float) print('finding clusters') codes, dist = scipy.cluster.vq.kmeans(ar, clusters) print('cluster centres:\n', codes) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] colour = ''.join(chr(int(c)) for c in peak) if (save_images): c = ar.copy() for i, code in enumerate(codes): c[scipy.r_[scipy.where(vecs == i)], :] = code misc.imsave("images64x64_recolored/" + str(idx) + ".png", c.reshape(*shape)) return idx, codes / 255
def find_a_dominant_color(image): # K-mean clustering to find the k most dominant color, from: # http://stackoverflow.com/questions/3241929/python-find-dominant-most-common-color-in-an-image n_clusters = 5 # Get image into a workable form im = image.copy() im = im.resize((150, 150)) # optional, to reduce time ar = scipy.misc.fromimage(im) im_shape = ar.shape ar = ar.reshape(scipy.product(im_shape[:2]), im_shape[2]) ar = np.float_(ar) # Compute clusters codes, dist = scipy.cluster.vq.kmeans(ar, n_clusters) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences # Get the indexes of the most frequent, 2nd most frequent, 3rd, ... sorted_idxs = np.argsort(counts) # Get the color peak = codes[sorted_idxs[1]] # get second most frequent color return [int(i) for i in peak.tolist()] # list comprehension to quickly cast everything to int
def extract_main_color4(): import binascii import struct from PIL import Image import numpy as np import scipy import scipy.misc import scipy.cluster NUM_CLUSTERS = 5 print('reading image') im = Image.open('d3.jpg') im = im.resize((150, 150)) # optional, to reduce time ar = np.asarray(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float) print('finding clusters') codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) print('cluster centres:\n', codes) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] colour = binascii.hexlify(bytearray(int(c) for c in peak)).decode('ascii') print('most frequent is %s (#%s)' % (peak, colour)) import imageio c = ar.copy() for i, code in enumerate(codes): c[scipy.r_[scipy.where(vecs == i)], :] = code imageio.imwrite('clusters.png', c.reshape(*shape).astype(np.uint8)) print('saved clustered image')
def get_dominant_color(image_path): ''' Parse image and return dominant color in image. @param image_path: Image path to parse. @return: Return dominant color, format as hexadecimal number. ''' # print 'reading image' im = Image.open(image_path) im = im.resize((150, 150)) # optional, to reduce time ar = scipy.misc.fromimage(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]) # print 'finding clusters' NUM_CLUSTERS = 5 codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) # print 'cluster centres:\n', codes vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] colour = ''.join(chr(c) for c in peak).encode('hex') # print 'most frequent is %s (#%s)' % (peak, colour) return "#%s" % (colour[0:6])
def color_clusters(cv2_im, NUM_CLUSTERS=3): im = ocv2pil(cv2_im) coef = 350 / min(im.size) im = im.resize((int(im.size[0] * coef), int(im.size[1] * coef))) # optional, to reduce time ar = np.asarray(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]).astype(float) #print('finding clusters') codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) #print('cluster centres:\n', codes) codes = codes.astype(np.uint8) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] colour = binascii.hexlify(bytearray(int(c) for c in peak)).decode('ascii') #print('most frequent is %s (#%s)' % (peak, colour)) import imageio c = ar.copy() for i, code in enumerate(codes): c[scipy.r_[scipy.where(vecs==i)],:] = code #imageio.imwrite('clusters.png', c.reshape(*shape).astype(np.uint8)) #print('saved clustered image') out = c.reshape(*shape).astype(np.uint8) out = pil2ocv(out) return out
def testHistgramdd(self): imgDds = mango.data.gaussian_noise(self.imgShape, mtype="tomo", mean=32000, stdd=100) se = mango.image.sphere_se(radius=3) meanImg = mango.image.mean_filter(imgDds, se) stddImg = mango.image.stdd_filter(imgDds, se) h, edges = mango.image.histogramdd([meanImg, stddImg], bins=[256,128]) self.assertEqual(2, len(h.shape)) self.assertEqual(h.shape[0], 256) self.assertEqual(h.shape[1], 128) self.assertEqual(sp.product(imgDds.shape), sp.sum(h))
def generate_scalar(self): """ breaks the image into a multi-dimensional array -> (ar) then breaks the rows up by NUM_CLUSTERS & color -> (codes) """ ar = fromimage(self.im) shape = ar.shape ar = ar.reshape(product(shape[:2]), shape[2]) float_ar = ar+0.0 codes, dist = kmeans(float_ar, self.NUM_CLUSTERS) return ar, codes
def dominant_color(img, clusters=5, size=50): """Group the colors in an image into like clusters, and return the central value of the largest cluster -- the dominant color.""" assert img.mode == 'RGB', 'RGB images only!' img.thumbnail((size, size)) imgarr = scipy.misc.fromimage(img) imgarr = imgarr.reshape(scipy.product(imgarr.shape[:2]), imgarr.shape[2]) colors, dist = vq.kmeans(imgarr.astype(np.float), clusters) vecs, dist = vq.vq(imgarr, colors) counts, bins = scipy.histogram(vecs, len(colors)) dominant_color = colors[counts.argmax()] return map(int, dominant_color) # Avoid returning np.uint8 type.
def getPalette(image): array = scipy.misc.fromimage(image) shape = array.shape array = array.reshape(scipy.product(shape[:2]), shape[2]) # Use K-Means to get the n most used colours colours, dist = scipy.cluster.vq.kmeans(array, COLOUR_PALETTE_SIZE) colours = colours.tolist() if len(colours) == COLOUR_PALETTE_SIZE: return colours else: return None
def determine_dominant_color_in_image(self, image): NUM_CLUSTERS = 5 # Convert image into array of values for each point. if image.mode == "1": image.convert("L") ar = numpy.array(image) # ar = scipy.misc.fromimage(image) shape = ar.shape # Reshape array of values to merge color bands. [[R], [G], [B], [A]] => [R, G, B, A] if len(shape) > 2: ar = ar.reshape(scipy.product(shape[:2]), shape[2]) # Get NUM_CLUSTERS worth of centroids. codes, _ = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) # Pare centroids, removing blacks and whites and shades of really dark and really light. original_codes = codes for low, hi in [(60, 200), (35, 230), (10, 250)]: codes = scipy.array( [ code for code in codes if not ( (code[0] < low and code[1] < low and code[2] < low) or (code[0] > hi and code[1] > hi and code[2] > hi) ) ] ) if not len(codes): codes = original_codes else: break # Assign codes (vector quantization). Each vector is compared to the centroids # and assigned the nearest one. vecs, _ = scipy.cluster.vq.vq(ar, codes) # Count occurences of each clustered vector. counts, bins = scipy.histogram(vecs, len(codes)) # Show colors for each code in its hex value. # colors = [''.join(chr(c) for c in code).encode('hex') for code in codes] # total = scipy.sum(counts) # print dict(zip(colors, [count/float(total) for count in counts])) # Find the most frequent color, based on the counts. index_max = scipy.argmax(counts) peak = codes[index_max] color = "".join(chr(c) for c in peak).encode("hex") return color[:6]
def most_colour(filename): colordb = ColorDB.get_colordb('rgb.txt') #r, g, b = (255, 251, 250) #nearest = colordb.nearest(r, g, b) NUM_CLUSTERS = 30 print 'reading image' im = Image.open(filename) im = im.resize((50, 50)) # optional, to reduce time im = im.convert('P', palette=Image.ADAPTIVE, colors=15) im = im.convert() ar = scipy.misc.fromimage(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]) print 'finding clusters' codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) print 'cluster centres:\n', codes vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent print codes print counts n = 0 for peak in codes: print "%s - %s" % (colordb.nearest(peak[0], peak[1], peak[2]), counts[n]) n += 1 peak = codes[index_max] pix = im.load() width, height = im.size topleft = pix[width - 1, height - 1] print pix print topleft tlcolor = ''.join(chr(c) for c in topleft).encode('hex') print "Top left pixel is %s (%s)" % (colordb.nearest(topleft[0], topleft[1], topleft[2]), tlcolor) colour = ''.join(chr(c) for c in peak).encode('hex') print 'most frequent is %s (#%s)' % (colordb.nearest(peak[0], peak[1], peak[2]), colour) print peak print topleft if colour == tlcolor: peak = codes[index_max - 1] colour = ''.join(chr(c) for c in peak).encode('hex') print 'New most frequent is %s (#%s)' % (peak, colour) print 'most frequent is %s (#%s)' % (peak, colour) return colordb.nearest(peak[0], peak[1], peak[2])
def get_pictures(required): gd_client = gdata.photos.service.PhotosService() photos = gd_client.SearchCommunityPhotos(required, limit='50') no_error = False j = 0 count = 0 purl1 = 'none' purl2 = 'none' purl3 = 'none' for photo in photos.entry: if j < 50: purl=photo.content.src NUM_CLUSTERS = 5 print 'reading image' URL = purl file = cStringIO.StringIO(urllib.urlopen(URL).read()) im = Image.open(file) im = im.resize((150, 150)) # optional, to reduce time ar = scipy.misc.fromimage(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]) print 'finding clusters' codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) print 'cluster centres:\n', codes vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] color = ''.join(chr(c) for c in peak).encode('hex') various=''.join(['#', color]) new_color=get_color_name(various, peak) new_name=webcolors.name_to_hex(new_color, spec='css3') print new_name print 'most frequent is %s (#%s)' % (peak, color) cat = col.lower() req = webcolors.hex_to_rgb(cat) new_cat=get_color_name(cat, req) print cat j += 1 print j if new_color == new_cat: no_error = True count += 1 print count if count == 1: purl1 = purl if count == 2: purl2 = purl if count == 3: purl3 = purl if j == 50 or count == 3: return purl1, purl2, purl3
def set_colors_from_image(self, image, num_colors=8): NUM_CLUSTERS = num_colors im = Image.open(image) im = im.resize((150, 150)) # to reduce time ar = scipy.misc.fromimage(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]) color_list, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) for r,g,b in color_list: color = RGBColor.objects.get(red=r, green=g, blue=b) self.colors.add(color)
def get_dominant_image_colors(image, num_clusters=4): """ Returns the dominant image color that isn't pure white or black. Uses kmeans on the colors. Returns the result as RGB hex strings in the format ['#rrggbb', '#rrggbb', ...]. :param image: PIL image or path """ if isinstance(image, basestring): image = Image.open(image) # downsample for speed im = image.resize((512, 512), Image.ANTIALIAS) # reshape ar0 = scipy.misc.fromimage(im) shape = ar0.shape npixels = scipy.product(shape[:2]) ar0 = ar0.reshape(npixels, shape[2]) # keep only nontransparent elements ar = ar0[ar0[:, 3] == 255][:, 0:3] try: # kmeans clustering codes, dist = scipy.cluster.vq.kmeans(ar, num_clusters) except: # kmeans sometimes fails -- if that is the case, use the mean color and # nothing else. arf = ar.astype(float) clamp = lambda p: max(0, min(255, int(p))) return ['#' + ''.join(['%0.2x' % clamp(arf[:, i].sum() / float(arf.shape[1])) for i in (0, 1, 2)])] vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences # sort by count frequency indices = [i[0] for i in sorted(enumerate(counts), key=lambda x:x[1], reverse=True)] # convert to hex strings colors = [''.join(chr(c) for c in code).encode('hex') for code in codes] results = [] for idx in indices: color = colors[idx] if color != 'ffffff' and color != '000000': results.append('#' + color) return results
def most_freq_color(pic): im = Image.open(pic).convert('RGB') im = im.resize((150, 150)) ar = scipy.misc.fromimage(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]) codes, dist = scipy.cluster.vq.kmeans(ar, 5) vecs, dist = scipy.cluster.vq.vq(ar, codes) counts, bins = scipy.histogram(vecs, len(codes)) index_max = scipy.argmax(counts) peak = codes[index_max] colour = ''.join(chr(c) for c in peak).encode('hex') print "most freq is %s (#%s)" % (peak, colour)
def main_color(image_src): image = Image.open(image_src) print(image) # Convert image into array of values for each point. ar = scipy.misc.fromimage(image) shape = ar.shape # Reshape array of values to merge color bands. if len(shape) > 2: ar = ar.reshape(scipy.product(shape[:2]), shape[2]) # Get NUM_CLUSTERS worth of centroids. codes, _ = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) # Pare centroids, removing blacks and whites and shades # of really dark and really light. original_codes = codes for low, hi in [(60, 200), (35, 230), (10, 250)]: codes = scipy.array([ code for code in codes if not ( (code[0] < low and code[1] < low and code[2] < low) or (code[0] > hi and code[1] > hi and code[2] > hi) ) ]) if not len(codes): codes = original_codes else: break # Assign codes (vector quantization). # Each vector is compared to the centroids # and assigned the nearest one. vecs, _ = scipy.cluster.vq.vq(ar, codes) # Count occurences of each clustered vector. counts, bins = scipy.histogram(vecs, len(codes)) # Show colors for each code in its hex value. colors = [''.join(chr(c) for c in code).encode('hex') for code in codes] total = scipy.sum(counts) color_dist = dict(zip(colors, [count/float(total) for count in counts])) print(color_dist) # Find the most frequent color, based on the counts. index_max = scipy.argmax(counts) peak = codes[index_max] return ''.join(chr(c) for c in peak).encode('hex')
def analyzeImage(self, image): color_band = scipy.misc.fromimage(image) shape = color_band.shape color_band = color_band.reshape(scipy.product(shape[:2]), shape[2]) self.log('generating clusters') codes, dist = kmeans(color_band, self.NUM_OF_CLUSTERS) self.log('Here are the cluster centres:') self.log(codes) vecs, dist = vq(color_band, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences return (codes, counts)
def linecolor(image_file): im = Image.open(image_file) #~ im = im.resize((150, 150)) # optional, to reduce time ar = scipy.misc.fromimage(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]) codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences index_max = scipy.argmax(counts) # find most frequent peak = codes[index_max] colour = ''.join(chr(c) for c in peak).encode('hex') return colour
def get_dominant_colors(im, nclusters = NUM_CLUSTERS): im = im.resize((150, 150)) im = im.point(lambda x: x >> 4 << 4) # 256^3 -> 16^3 ar = scipy.misc.fromimage(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]) codes, dist = scipy.cluster.vq.kmeans(ar,nclusters) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences order = counts.argsort()[::-1] sorted_colors = [] for i in range(len(counts)): peak = codes[order[i]] sorted_colors.append(tuple(peak[:3])) return sorted_colors
def __init__(self, dist): self.posteriors = {} for peakidx, peak in enumerate(ppeaks): dim = len(peak)*(6,) self.posteriors[peak] = scipy.zeros( (10,)+dim, 'f') agnostic = scipy.ones(dim, 'f')/scipy.product(dim) for gi, g in enumerate(pcalls): cslice = (gi,)+len(dim)*(slice(None),) self.posteriors[peak][cslice] = ( dist.typedists[g][peakidx]* dist.scoredists.get('%s-%s' % (g, peak), agnostic).reshape(dim)) self.posteriors[peak] /= sum(self.posteriors[peak]) self.memo = {}