Beispiel #1
0
    def render(self, width, height, results):
        if not is_gpu_accelerated():
            self._logger.error('No GPU acceleration is available.')
            return

        image = Image.new('RGB', (width, height))

        iterations_gpu, z_values_gpu, dc = results

        colors = np.empty(width * height, np.int32)
        colors_gpu = gpuarray.to_gpu(colors)

        dx, mx = divmod(width, self._block_size[0])
        dy, my = divmod(height, self._block_size[1])
        grid_size = ((dx + (mx > 0)), (dy + (my > 0)))

        self._get_pixel_color(colors_gpu,
                              self._color_scheme_gpu,
                              iterations_gpu,
                              z_values_gpu,
                              np.int32(width),
                              np.int32(height),
                              np.float32(dc),
                              block=self._block_size,
                              grid=grid_size)

        colors = colors_gpu.get()

        # This is really slow, must be optimized
        image.putdata(colors.tolist())

        return image
def get_colors_and_cmap(K):
    if K <= 10:
        colors = np.array(plt.cm.tab10.colors)[:K]
        colors_list = colors.tolist()
    elif K <= 20:
        colors = np.array(plt.cm.tab20.colors)[:K]
        colors_list = colors.tolist()
    else:
        vals = np.linspace(0, 1, 10 * K)[10 * np.arange(K)]
        colors = plt.cm.tab20(vals)
        colors_list = colors.tolist()

    cvals = np.arange(K)

    norm = plt.Normalize(min(cvals), max(cvals))
    tuples = list(zip(map(norm, cvals), colors_list))
    cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", tuples, K)

    return colors, cmap
def for_categories(n, cmap='Spectral', avoid_white=True):
    cm = matplotlib.cm.get_cmap(cmap)
    colors = cm(numpy.linspace(0, 1, n))
    colors_order = []
    for i in range(n / 3):
        colors_order += [i, i + n / 3, i + 2 * n / 3]
    colors = colors[colors_order]

    if avoid_white:
        hsv_colors = matplotlib.colors.rgb_to_hsv(colors[:, :3])
        hsv_colors[:, 2] = numpy.where(hsv_colors[:, 2] > 0.9, 0.9,
                                       hsv_colors[:, 2])
        colors = matplotlib.colors.hsv_to_rgb(hsv_colors)
    return colors.tolist()
def create(n, cmap='YlGnBu', avoid_white=True):
    """
	cmap
	"""
    cm = matplotlib.cm.get_cmap(cmap)
    colors = cm(numpy.linspace(0, 1, cm.N))
    gray = grayify(colors)
    sign = numpy.sign(gray[1:] - gray[:-1])
    if not numpy.all(sign[sign != 0] == sign[sign != 0][0]):
        warnings.warn('This cmap ("%s") is not monotonic in grayscale!' % cmap)
    colors = cm(numpy.linspace(0, 1, n))
    if avoid_white:
        hsv_colors = matplotlib.colors.rgb_to_hsv(colors[:, :3])
        hsv_colors[:, 2] = numpy.where(hsv_colors[:, 2] > 0.9, 0.9,
                                       hsv_colors[:, 2])
        #hsv_colors[:,1] = numpy.where(hsv_colors[:,1] < 0.5, 0.5, hsv_colors[:,1])
        colors = matplotlib.colors.hsv_to_rgb(hsv_colors)
    #colors = grayify(colors)
    return colors.tolist()