def lab2ind(im, colors=256): """convert a Lab image to indexed colors :param a: nparray (x,y,n) containing image :param colors: int number of colors or predefined Palette :ref: http://scikit-learn.org/stable/auto_examples/cluster/plot_color_quantization.html """ #http://stackoverflow.com/questions/10818546/finding-index-of-nearest-point-in-numpy-arrays-of-x-and-y-coordinates if isinstance(colors, int): p = palette(im, colors) # pal = [Color(c, 'lab') for c in p] else: pal = colors p = [c.lab for c in itertools2.flatten(pal)] w, h, d = im.shape s = w * h #number of pixels flat = np.reshape(im, (s, d)) from scipy.spatial import cKDTree as KDTree #compiled is MUCH faster mytree = KDTree(p) _, indexes = mytree.query(flat) im = indexes.reshape(w, h) return im, pal
def lab2ind(im,colors=256): """convert a Lab image to indexed colors :param a: nparray (x,y,n) containing image :param colors: int number of colors or predefined Palette :ref: http://scikit-learn.org/stable/auto_examples/cluster/plot_color_quantization.html """ #http://stackoverflow.com/questions/10818546/finding-index-of-nearest-point-in-numpy-arrays-of-x-and-y-coordinates if isinstance(colors,int): p=palette(im,colors) # pal=[Color(c,'lab') for c in p] else: pal=colors p=[c.lab for c in itertools2.flatten(pal)] w, h, d = im.shape s=w*h #number of pixels flat = np.reshape(im, (s, d)) from scipy.spatial import cKDTree as KDTree #compiled is MUCH faster mytree = KDTree(p) _, indexes = mytree.query(flat) im=indexes.reshape(w,h) return im,pal
1, lambda n:len(math2.sieve(n+1,oneisprime=True)), lambda n:True, #all integers are in this sequence. desc="pi(n), the number of primes <= n. Sometimes called PrimePi(n)" ) """ A018239=A006862.filter( math2.is_prime, desc='Primorial primes: form product of first k primes and add 1, then reject unless prime.' ) A007504=A000040.accumulate() A001223=A000040.pairwise(operator.sub) A077800=Sequence(itertools2.flatten(math2.twin_primes())) A001097=A077800.unique() A001359=Sequence(itertools2.itemgetter(math2.twin_primes(),0),desc="Lesser of twin primes.") A006512=Sequence(itertools2.itemgetter(math2.twin_primes(),1),desc="Greater of twin primes.") A037074=Sequence(map(math2.mul,math2.twin_primes()), desc="Numbers that are the product of a pair of twin primes") def count_10_exp(iterable): """generates number of iterable up to 10^n.""" l=10 c=0 for n in iterable: if n>l:
def _hash_result(result): return math2.num_from_digits(itertools2.flatten(result), 2)
A000720=Sequence( 1, lambda n:len(math2.sieve(n+1,oneisprime=True)), lambda n:True, #all integers are in this sequence. desc="pi(n), the number of primes <= n. Sometimes called PrimePi(n)" ) """ A018239=A006862.filter( math2.is_prime, desc='Primorial primes: form product of first k primes and add 1, then reject unless prime.' ) A001223=A000040.pairwise(operator.sub) A077800=Sequence(itertools2.flatten(math2.twin_primes())) A001097=Sequence(itertools2.unique_sorted(A077800)) A001359=Sequence(itertools2.itemgetter(math2.twin_primes(),0),desc="Lesser of twin primes.") A006512=Sequence(itertools2.itemgetter(math2.twin_primes(),1),desc="Greater of twin primes.") A037074=Sequence(six.moves.map(math2.mul,math2.twin_primes()), desc="Numbers that are the product of a pair of twin primes") def count_10_exp(iterable): """generates number of iterable up to 10^n.""" l=10 c=0 for n in iterable: if n>l: