示例#1
0
def main():
    colors = {
        'red'   : '#f00',
        'white' : '#fff',
    }

    float_colors = { }

    for key, value in colors.items():
        color = parse_color(value)
        colors[key] = np.array([ color ], dtype=Pixel)
        float_colors[key] = np.array(tuple(c / 255. for c in color), dtype=FloatPixel)

    image = Tile((0, 0), (512, 512), sample_rate=4, dtype=FloatPixel)
    image.buffer[:,:] = float_colors['white']

    tile_width, tile_height = image.buffer.shape

    mesh = MicropolygonMesh((1,1))
    mesh_width, mesh_height = mesh.buffer.shape

    margin = 16
    width = 128
    right  = image.shape[0]
    top    = image.shape[1]


    buffer = np.array([
            [
                (right - margin - width, top - margin, 1, 1, 1.5, 0, 0, 1),
                (right - margin        , top - margin, 1, 1, 0  , 0, 0, 1)
            ],
            [
                (margin        , margin, 1, 1, .5, 0, 0, 1),
                (margin + width, margin, 1, 1, 0 , 0, 0, 1)
            ],
        ],
        dtype=np.float32
    )

    mesh.buffer.view(dtype=(np.float32, 8))[:] = buffer

    fill_micropolygon_mesh(
        mesh_width, mesh_height,
        generate_numpy_begin(mesh.buffer),
        tile_width, tile_height,
        generate_numpy_begin(image.coordinate_image),
        generate_numpy_begin(image.buffer)
    )

    buffer = array_view(image.downsample(1))
    buffer = np.clip(buffer, 0., 1.)
    buffer = (255. * buffer).astype(dtype=np.uint8)

    save_array_as_image(pixel_view(buffer), 'render/002_micropolygon.tiff', 'RGBA')
示例#2
0
def test_parse_color():
    tests = [
        ('#f00',      (255, 0, 0, 255)),
        ('0f0',       (0, 255, 0, 255)),
        ('#00f0',     (0, 0, 255, 0)),
        ('7f00',      (119, 255, 0, 0)),
        ('#ff0000',   (255, 0, 0, 255)),
        ('00f800',    (0, 248, 0, 255)),
        ('#0000acac', (0, 0, 172, 172)),
        ('abcdef12',  (171, 205, 239, 18)),
    ]

    for input, expected in tests:
        assert parse_color(input) == expected
示例#3
0
def palette():
    colors = {
        'clear'  : '#0000',
        'white'  : '#fff',
        'red'    : '#7f0000',
        'black'  : '#000',
        'blue' : '#0000bf',
        # 'blue'   : '#0000ff',
        'orange' : '#FF9701'
    }

    colors = {
        key : np.array([c / 255. for c in parse_color(value)], dtype=np.float32)
        for key, value
        in colors.items()
    }

    return colors
示例#4
0
文件: tasks.py 项目: bracket/handsome
def render_tile():
    import random

    mode = 'RGBA'
    image_shape = (640, 480)

    image = Tile((0,0), image_shape)

    r = random.SystemRandom()

    colors = {
        'white'  : '#ffff',
        'black'  : '#000f',
        '205A8C' : '205A8C',
        '6596BF' : '6596BF',
        '98F2EC' : '98F2EC',
        'BF6865' : 'BF6865',
        'F4DBD6' : 'F4DBD6',
    }

    for name, color in colors.items():
        colors[name] = parse_color(color)

    image.buffer[:,:] = colors['white']

    tile_colors = [ color for name, color in colors.items() if name not in ('black', 'white') ]
    # tile = Tile((0, 0), (100, 100))
    tile = Tile((0, 0), (16, 16))

    for x in range(0, image_shape[0], 16):
        for y in range(0, image_shape[1], 16):
            # x = int(r.random() * image_shape[0])
            # y = int(r.random() * image_shape[1])
            tile.set_origin((x, y))
            tile.buffer[:] = r.sample(tile_colors, 1)

            i, t = image.intersection_slices(tile)
            image.buffer[i] = tile.buffer[t]

    save_array_as_image('test_tile.gif', image.buffer, 'RGBA')
示例#5
0
文件: tasks.py 项目: bracket/handsome
def blocktopus():
    import random
    import math
    r = random.SystemRandom()

    mode = 'RGBA'
    image_shape = (1280, 960)
    # image_shape = (640, 480)

    image = Tile((0, 0), image_shape)

    colors = {
        'white'        : '#ffff',
        'black'        : '#000f',
        '205A8C'       : '205A8C',
        '6596BF'       : '6596BF',
        '98F2EC'       : '98F2EC',
        'BF6865'       : 'BF6865',
        'F4DBD6'       : 'F4DBD6',
        'dark_blue'    : '05115B',
        'light_blue'   : '60CAF0',
        'brown_yellow' : 'DAB32B',
        'brown'        : '855E14',
        'yellow'       : 'FFDD06',
    }

    for name, color in colors.items():
        colors[name] = parse_color(color)

    def make_tile(color):
        tile = Tile((0, 0), (16, 16))
        tile.buffer[:] = color
        return tile

    blocktopus_colors = (
        'brown',
        'brown_yellow',
        'dark_blue',
        'light_blue',
        'yellow',
    )

    tiles = { }

    def get_tile(tile_id):
        tile = tiles.get(tile_id)
        if tile is not None:
            return tile
        
        color = r.choice(blocktopus_colors)
        tiles[tile_id] = out = make_tile(color)
        out.buffer[:] = colors[color]
        return out

    spine = Line(
        point(.1 * image_shape[0], .5 * image_shape[1]),
        point(.9 * image_shape[0], .5 * image_shape[1])
    )

    direction = normalize(spine.direction)
    up = normalize(rotate_left(spine.direction))

    steps = 30 
    step_size = 1. / steps
    frequency = 2.5 * 2. * math.pi * step_size

    frames = 10 * 30

    for frame in range(frames):
        image.buffer[:] = colors['white']

        phase = 2 * 2. * math.pi * frame / frames
        phase_offset = .5 - math.cos(phase)

        for i in range(steps):
            width_factor = (1. - float(i) / (steps - 1))
            width = width_factor * .15 * image_shape[1]

            s = math.cos(frequency * i + phase) + phase_offset

            base = spine(i * step_size)

            offsets = [
                s * width * up,
                (s - 1) * width * up,
            ]

            for (j, offset) in enumerate(offsets):
                p = base + offset
                tile = get_tile((i, j))
                tile.set_origin(p[:2])

                intersection = image.intersection_slices(tile)
                if intersection is None: continue

                m, t = intersection
                image.buffer[m] = tile.buffer[t]

        path = 'blocktopus/frame_{frame:03}.tiff'.format(frame=frame)
        save_array_as_image(path, image.buffer, 'RGBA')