예제 #1
0
def eread(document: vp.Document, filename: str):
    # populate the vp_source[s] properties
    document.set_property(vp.METADATA_FIELD_SOURCE,
                          pathlib.Path(filename).absolute())
    document.add_to_sources(filename)

    pattern = EmbPattern(filename)
    for stitches, color in pattern.get_as_stitchblock():
        if len(stitches) == 0:
            continue
        lc = vp.LineCollection()
        lc.scale(1.0 / _EMB_SCALE_FACTOR)
        stitch_block = np.asarray(stitches, dtype="float")
        stitch_block = stitch_block[..., 0] + 1j * stitch_block[..., 1]
        lc.append(stitch_block)
        lc.set_property(vp.METADATA_FIELD_COLOR, vp.Color(color.hex_color()))
        document.add(lc, with_metadata=True)
    return document
예제 #2
0
def pixelart(document: vp.Document, image, mode, pen_width: float):
    """Plot pixel art.

    Two modes are available:
    - "big" creates a square spiral for each pixel
    - "line" create single horizontal lines for contiguous pixels of the same color
    """

    # this should be dealt with by add_to_source() in a future release
    document.set_property(vp.METADATA_FIELD_SOURCE, pathlib.Path(image).absolute())
    document.add_to_sources(image)

    img = imageio.imread(image, pilmode="RGBA")
    colors = np.unique(img[:, :, 0:3][img[:, :, 3] == 255], axis=0)

    if mode == "big":
        for col_idx, color in enumerate(colors, start=1):
            indice_i, indice_j = np.nonzero(
                np.all(img[:, :, 0:3] == color, axis=2) & (img[:, :, 3] == 255)
            )

            lines = []
            for i, j in zip(indice_j, indice_i):
                line = np.array(PIXEL_TRAJECTORY) + i * PIXEL_OFFSET + j * PIXEL_OFFSET * 1j
                line *= pen_width

                lines.append(line)

            document.add(vp.LineCollection(lines), col_idx)
    elif mode == "line":
        for row_idx, line in enumerate(img):
            start = 0
            while True:
                while start < len(line) and line[start, 3] != 255:
                    start += 1

                # loop ending condition
                if start == len(line):
                    break

                # find the end of the current pixel run
                end = start
                while (
                    end < len(line)
                    and np.all(line[end, 0:3] == line[start, 0:3])
                    and line[end, 3] == 255
                ):
                    end += 1

                #
                layer_id = np.where(np.all(colors == line[start, 0:3], axis=1))[0][0] + 1
                segment = np.array([row_idx * 1j + (start - 0.1), row_idx * 1j + (end - 0.9)])
                segment *= pen_width
                document.add(vp.LineCollection([segment]), layer_id)

                # move to the next line
                start = end

    for col_idx, color in enumerate(colors, start=1):
        document.layers[col_idx].set_property(vp.METADATA_FIELD_COLOR, vp.Color(*color))
        document.layers[col_idx].set_property(vp.METADATA_FIELD_PEN_WIDTH, pen_width)

    return document