Exemplo n.º 1
0
 def rectangle_motion(self, event):
     x0, y0 = (self.previous_point[0], self.previous_point[1])
     x1, y1 = (event.x, event.y)
     paper = copy.copy(self.img)
     self.rectangleImg = draw.rectangle((x0, y0), (x1, y1), brush_color, paper, self.defaultState)
     self.canvas.create_image(self.paper_width / 2, self.paper_height / 2, image=self.rectangleImg)
     self.defaultState = 0
Exemplo n.º 2
0
 def on_button_choosing_place_motion(self, event):
     x0, y0 = (self.previous_point[0], self.previous_point[1])
     x1, y1 = (event.x, event.y)
     paper = copy.copy(self.img)
     self.rectangleImg = draw.rectangle((x0, y0), (x1, y1), "blue", paper, self.defaultState)
     self.canvas.create_image(self.paper_width / 2, self.paper_height / 2, image=self.rectangleImg)
     self.defaultState = 0
Exemplo n.º 3
0
def shutdown(args):
    scr = args["screen"]  # Screen

    # Not actually shutting down computer or anything..
    box = draw.rectangle(scr, int(os.get_terminal_size()[0]/2), 2)
    box.draw(int(scr.get_width()/2 - box.width/2),
             int(scr.get_height()/2 - box.height/2))
    box.write(box.width/2-len("Shutting down...")/2, 1, "Shutting down...")

    time.sleep(5)
    return True, None
Exemplo n.º 4
0
def rect_middle(height, scr):
    """ For drawing rectangles in the middle """

    width = int(scr.get_width() / 2)

    rectangle_x = int(scr.get_width() / 2 - width / 2)
    rectangle_y = int(scr.get_height() / 2)

    rect = draw.rectangle(scr, width, height)

    return rect, rectangle_x, rectangle_y
Exemplo n.º 5
0
def label2rgb(
    label,
    img=None,
    alpha=0.5,
    label_names=None,
    font_size=30,
    thresh_suppress=0,
    colormap=None,
    loc="centroid",
    font_path=None,
):
    """Convert label to rgb.

    Parameters
    ----------
    label: numpy.ndarray, (H, W), int
        Label image.
    img: numpy.ndarray, (H, W, 3), numpy.uint8
        RGB image.
    alpha: float
        Alpha of RGB (default: 0.5).
    label_names: list of string
        Label id to label name.
    font_size: int
        Font size (default: 30).
    thresh_suppress: float
        Threshold of label ratio in the label image.
    colormap: numpy.ndarray, (M, 3), numpy.uint8
        Label id to color.
        By default, :func:`~imgviz.label_colormap` is used.
    loc: string
        Location of legend (default: 'centroid').
        'lt' and 'rb' are supported.
    font_path: str
        Font path.

    Returns
    -------
    res: numpy.ndarray, (H, W, 3), numpy.uint8
        Visualized image.

    """
    if colormap is None:
        colormap = label_colormap()

    res = colormap[label]

    random_state = np.random.RandomState(seed=1234)

    mask_unlabeled = label < 0
    res[mask_unlabeled] = random_state.rand(*(mask_unlabeled.sum(), 3)) * 255

    if img is not None:
        if img.ndim == 2:
            img = color_module.gray2rgb(img)
        res = (1 - alpha) * img.astype(float) + alpha * res.astype(float)
        res = np.clip(res.round(), 0, 255).astype(np.uint8)

    if label_names is None:
        return res

    unique_labels = np.unique(label)
    unique_labels = unique_labels[unique_labels != -1]
    unique_labels = [l for l in unique_labels if label_names[l] is not None]
    if len(unique_labels) == 0:
        return res

    if loc == "centroid":
        for label_i in unique_labels:
            mask = label == label_i
            if 1.0 * mask.sum() / mask.size < thresh_suppress:
                continue
            y, x = np.array(_center_of_mass(mask), dtype=int)

            if label[y, x] != label_i:
                Y, X = np.where(mask)
                point_index = np.random.randint(0, len(Y))
                y, x = Y[point_index], X[point_index]

            text = label_names[label_i]
            height, width = draw_module.text_size(text,
                                                  size=font_size,
                                                  font_path=font_path)
            color = color_module.get_fg_color(res[y, x])
            res = draw_module.text(
                res,
                yx=(y - height // 2, x - width // 2),
                text=text,
                color=color,
                size=font_size,
                font_path=font_path,
            )
    elif loc in ["rb", "lt"]:
        text_sizes = np.array([
            draw_module.text_size(label_names[l],
                                  font_size,
                                  font_path=font_path) for l in unique_labels
        ])
        text_height, text_width = text_sizes.max(axis=0)
        legend_height = text_height * len(unique_labels) + 5
        legend_width = text_width + 20 + (text_height - 10)

        height, width = label.shape[:2]
        legend = np.zeros((height, width, 3), dtype=np.uint8)
        if loc == "rb":
            aabb2 = np.array([height - 5, width - 5], dtype=float)
            aabb1 = aabb2 - (legend_height, legend_width)
        elif loc == "lt":
            aabb1 = np.array([5, 5], dtype=float)
            aabb2 = aabb1 + (legend_height, legend_width)
        else:
            raise ValueError("unexpected loc: {}".format(loc))
        legend = draw_module.rectangle(legend,
                                       aabb1,
                                       aabb2,
                                       fill=(255, 255, 255))

        alpha = 0.5
        y1, x1 = aabb1.round().astype(int)
        y2, x2 = aabb2.round().astype(int)
        res[y1:y2,
            x1:x2] = (alpha * res[y1:y2, x1:x2] + alpha * legend[y1:y2, x1:x2])

        for i, l in enumerate(unique_labels):
            box_aabb1 = aabb1 + (i * text_height + 5, 5)
            box_aabb2 = box_aabb1 + (text_height - 10, text_height - 10)
            res = draw_module.rectangle(res,
                                        aabb1=box_aabb1,
                                        aabb2=box_aabb2,
                                        fill=colormap[l])
            res = draw_module.text(
                res,
                yx=aabb1 + (i * text_height, 10 + (text_height - 10)),
                text=label_names[l],
                size=font_size,
                font_path=font_path,
            )
    else:
        raise ValueError("unsupported loc: {}".format(loc))

    return res