Пример #1
0
def convert(a, source, target, **kwargs):
    """convert an image between modes, eventually using intermediary steps
    :param a: nparray (x,y,n) containing image
    :param source: string : key of source image mode in modes
    :param target: string : key of target image mode in modes
    """
    import networkx as nx  # http://networkx.github.io/
    source, target = modes[source.upper()], modes[target.upper()]
    a = np.clip(a, source.min, source.max, out=a)
    try:
        path = converters.shortest_path(source.name, target.name)
    except nx.exception.NetworkXError:
        raise NotImplementedError('no conversion between %s and %s modes' %
                                  (source.name, target.name))

    for u, v in itertools2.pairwise(path):
        if u == v: continue  #avoid converting from gray to gray
        try:
            a = converters[u][v][0]['f'](a, **kwargs)
        except TypeError as e:
            if kwargs:  #maybe the converter doesn't support args ? retry!
                a = converters[u][v][0]['f'](a)
            else:
                raise (e)

    try:
        a = skimage.util.dtype.convert(a, target.type)
    except ValueError as e:  #probably a tuple (indexed, palette)
        pass  #do not force int type here
        #a=tuple((skimage.util.dtype.convert(a[0],target.type),a[1]))
    return a  #isn't it beautiful ?
Пример #2
0
    def convert(self, target, **kwargs):
        """ 
        :param target: str of desired colorspace, or none for default
        :return: color in target colorspace
        """
        import networkx as nx
        target=target.lower() if target else self.space
        if target not in self._values:
            try:
                path=converters.shortest_path(self.space, target)
            except nx.exception.NetworkXNoPath:
                raise NotImplementedError(
                    'no conversion between %s and %s color spaces'
                    %(self.space, target)
                )
            kwargs['illuminant']=self.illuminant # to avoid incoherent cached values
            for u,v in itertools2.pairwise(path):
                if v not in self._values:
                    edge=converters[u][v][0]
                    c=edge['f'](self._values[u],**kwargs)

                    if itertools2.isiterable(c): #but not a string
                        c=tuple(map(float,c))
                    self._values[v]=c
        return self._values[target]
Пример #3
0
    def convert(self, target, **kwargs):
        """ 
        :param target: str of desired colorspace, or none for default
        :return: color in target colorspace
        """
        import networkx as nx
        target = target.lower() if target else self.space
        if target not in self._values:
            try:
                path = converters.shortest_path(self.space, target)
            except nx.exception.NetworkXNoPath:
                raise NotImplementedError(
                    'no conversion between %s and %s color spaces' %
                    (self.space, target))
            kwargs[
                'illuminant'] = self.illuminant  # to avoid incoherent cached values
            for u, v in itertools2.pairwise(path):
                if v not in self._values:
                    edge = converters[u][v][0]
                    c = edge['f'](self._values[u], **kwargs)

                    if itertools2.isiterable(c):  #but not a string
                        c = tuple(map(float, c))
                    self._values[v] = c
        return self._values[target]
Пример #4
0
def convert(a,source,target,**kwargs):
    """convert an image between modes, eventually using intermediary steps
    :param a: nparray (x,y,n) containing image
    :param source: string : key of source image mode in modes
    :param target: string : key of target image mode in modes
    """
    import networkx as nx # http://networkx.github.io/
    source,target=modes[source.upper()],modes[target.upper()]
    a=np.clip(a, source.min, source.max, out=a)
    try:
        path=converters.shortest_path(source.name, target.name)
    except nx.exception.NetworkXError:
        raise NotImplementedError(
            'no conversion between %s and %s modes'
            %(source.name, target.name)
        )

    for u,v in itertools2.pairwise(path):
        if u==v: continue #avoid converting from gray to gray
        try:
            a=converters[u][v][0]['f'](a,**kwargs)
        except TypeError as e:
            if kwargs: #maybe the converter doesn't support args ? retry!
                a=converters[u][v][0]['f'](a)
            else:
                raise(e)

    try:
        a=skimage.util.dtype.convert(a,target.type)
    except ValueError as e: #probably a tuple (indexed, palette)
        pass #do not force int type here
        #a=tuple((skimage.util.dtype.convert(a[0],target.type),a[1]))
    return a #isn't it beautiful ?
Пример #5
0
def convert(color,source,target):
    """convert a color between colorspaces,
    eventually using intermediary steps
    """
    source,target=source.lower(),target.lower()
    if source==target: return color
    path=converters.shortest_path(source, target)
    for u,v in itertools2.pairwise(path):
        color=converters[u][v][0]['f'](color)
    return color #isn't it beautiful ?
Пример #6
0
def convert(color, source, target):
    """convert a color between colorspaces,
    eventually using intermediary steps
    """
    source, target = source.lower(), target.lower()
    if source == target: return color
    path = converters.shortest_path(source, target)
    for u, v in itertools2.pairwise(path):
        color = converters[u][v][0]['f'](color)
    return color  #isn't it beautiful ?
Пример #7
0
 def pairwise(self, op, skip_first=False):
     return Sequence(itertools2.pairwise(self, op))
Пример #8
0
 def pairwise(self,op,skip_first=False):
     return Sequence(itertools2.pairwise(self,op))