Ejemplo n.º 1
0
 def draw(self, draw: ImageDraw):
     if not self.show:
         return
     horizontal_pad = self.width // 25
     bottom_pad = self.height // 4
     draw.line((self.abs_col + horizontal_pad, self.abs_row + self.height -
                bottom_pad, self.abs_col + self.width - horizontal_pad,
                self.abs_row + self.height - bottom_pad),
               fill=self.foreground)
     text_w, text_h = self.font.getsize(' ')
     polygon_pts = ((self.abs_col + horizontal_pad,
                     self.abs_row + self.height - bottom_pad),
                    (self.abs_col + horizontal_pad,
                     self.abs_row + self.height - bottom_pad - text_h),
                    (self.abs_col + horizontal_pad + text_w * 8,
                     self.abs_row + self.height - bottom_pad - text_h),
                    (self.abs_col + horizontal_pad + text_w * 9,
                     self.abs_row + self.height - bottom_pad))
     date_str = datetime.datetime.strftime(self.date, ' %b %d')
     draw.polygon(polygon_pts, fill=self.foreground)
     draw.text((self.abs_col + horizontal_pad,
                self.abs_row + self.height - bottom_pad - text_h),
               date_str,
               fill=self.background,
               font=self.font)
     event_max_chars = (self.width - 2 * horizontal_pad) * 4 // 5 // text_w
     if len(self.event) > event_max_chars:
         self.event = self.event[:event_max_chars - 3] + '...'
     draw.text(
         (self.abs_col + (self.width - 2 * horizontal_pad) // 5 +
          horizontal_pad, self.abs_row + self.height - bottom_pad - text_h),
         self.event,
         fill=self.foreground,
         font=self.font)
Ejemplo n.º 2
0
    def draw(self, draw: ImageDraw):
        pen, brush = self._get_pen_brush()

        if hasattr(self, 'de') and self.de > 0:
            brush_s1 = aggdraw.Brush(fade_color(self.fill, self.shade))
            brush_s2 = aggdraw.Brush(fade_color(self.fill, 2 * self.shade))

            draw.line([
                self.x1 + self.de, self.y1 - self.de, self.x1 + self.de,
                self.y2 - self.de
            ], pen)
            draw.line([self.x1 + self.de, self.y2 - self.de, self.x1, self.y2],
                      pen)
            draw.line([
                self.x1 + self.de, self.y2 - self.de, self.x2 + self.de,
                self.y2 - self.de
            ], pen)

            draw.polygon([
                self.x1, self.y1, self.x1 + self.de, self.y1 - self.de,
                self.x2 + self.de, self.y1 - self.de, self.x2, self.y1
            ], pen, brush_s1)

            draw.polygon([
                self.x2 + self.de, self.y1 - self.de, self.x2, self.y1,
                self.x2, self.y2, self.x2 + self.de, self.y2 - self.de
            ], pen, brush_s2)

        draw.rectangle([self.x1, self.y1, self.x2, self.y2], pen, brush)
Ejemplo n.º 3
0
def draw_block(img: ImageDraw, points, x, y, block_size, color, angle):
    if angle > 0:
        # 0 角度不需要转换
        # 中心坐标与 x、y 的距离,方便下面指定中心坐标(x+m, y+m),
        # 0.5 的偏移值不能少,否则坐标靠右,非正中央
        m = block_size / 2 - 0.5
        points = rotate(points, x + m, y + m, angle)

    img.polygon(points, fill=color)
Ejemplo n.º 4
0
def draw_tangram(draw: ImageDraw, x: int, y: int, tangram: str):
    for i, letter in enumerate(tangram):
        row = int(i / 3)
        col = i % 3

        square_x_start = x + col*TANGRAM_SQUARE_SIZE
        square_y_start = y + row*TANGRAM_SQUARE_SIZE
        square_x_end = square_x_start+TANGRAM_SQUARE_SIZE
        square_y_end = square_y_start+TANGRAM_SQUARE_SIZE
        if is_color(letter):
            draw.rectangle([
                square_x_start,
                square_y_start,
                square_x_end,
                square_y_end],
                fill=COLORS[letter],
                outline=(0, 0, 0),
                width=TANGRAM_SQUARE_BORDER
            )
        else:
            draw.rectangle([
                square_x_start,
                square_y_start,
                square_x_end,
                square_y_end],
                fill=(255, 255, 255),
                outline=(0, 0, 0),
                width=1
            )
            square_coords = [
                (square_x_start+TANGRAM_SQUARE_BORDER, square_y_start+TANGRAM_SQUARE_BORDER),
                (square_x_start+TANGRAM_SQUARE_BORDER, square_y_end-TANGRAM_SQUARE_BORDER),
                (square_x_end-TANGRAM_SQUARE_BORDER, square_y_end-TANGRAM_SQUARE_BORDER),
                (square_x_end-TANGRAM_SQUARE_BORDER, square_y_start+TANGRAM_SQUARE_BORDER),
            ]
            if letter == "U":
                square_coords.pop(3)
            elif letter == "S":
                square_coords.pop(2)
            elif letter == "D":
                square_coords.pop(1)
            else:
                square_coords.pop(0)
            draw.polygon(square_coords, fill=(0, 0, 0))
Ejemplo n.º 5
0
 def draw(self, draw: ImageDraw) -> None:
     super().draw(draw)
     if not self.show:
         return
     horizontal_pad = self.width // 25
     bottom_pad = self.height // 4
     draw.line((self.abs_col + horizontal_pad, self.abs_row + self.height -
                bottom_pad, self.abs_col + self.width - horizontal_pad,
                self.abs_row + self.height - bottom_pad),
               fill=self.foreground)
     text_w, text_h = self.font.getsize(' ')
     # How many character's size the tab will take
     tab_width_char = 14
     polygon_pts = ((self.abs_col + horizontal_pad,
                     self.abs_row + self.height - bottom_pad),
                    (self.abs_col + horizontal_pad,
                     self.abs_row + self.height - bottom_pad - text_h),
                    (self.abs_col + horizontal_pad + text_w *
                     (tab_width_char - 1),
                     self.abs_row + self.height - bottom_pad - text_h),
                    (self.abs_col + horizontal_pad +
                     text_w * tab_width_char,
                     self.abs_row + self.height - bottom_pad))
     week_day_str = WEEK_DAYS[self.date.weekday()]
     date_str = '%s, %s' % (datetime.datetime.strftime(
         self.date, ' %b %d'), week_day_str)
     draw.polygon(polygon_pts, fill=self.foreground)
     draw.text((self.abs_col + horizontal_pad,
                self.abs_row + self.height - bottom_pad - text_h),
               date_str,
               fill=self.background,
               font=self.font)
     # We save three char's space between tab and event text
     event_max_chars = ((self.width - 2 * horizontal_pad) // text_w -
                        tab_width_char - 3)
     if len(self.event) > event_max_chars:
         self.event = self.event[:event_max_chars - 3] + '...'
     draw.text(
         (self.abs_col + text_w * (tab_width_char + 3) + horizontal_pad,
          self.abs_row + self.height - bottom_pad - text_h),
         self.event,
         fill=self.foreground,
         font=self.font)
Ejemplo n.º 6
0
def display_shapefile(name, iwidth=500, iheight=500):
    import shapefile
    from PIL import Image, ImageDraw
    r = shapefile.Reader(name)
    mleft, mbottom, mright, mtop = r.bbox
    # map units
    mwidth = mright - mleft
    mheight = mtop - mbottom
    # scale map units to image units
    hscale = iwidth / mwidth
    vscale = iheight / mheight
    img = Image.new("RGB", (iwidth, iheight), "white")
    draw = ImageDraw(img)
    for shape in r.shapes():
        pixels = [(int(iwidth - ((mright - x) * hscale)),
                   int((mtop - y) * vscale)) for x, y, in shape.points]
        if shape.shapeType == shapefile.POLYGON:
            draw.polygon(pixels, outline='black')
        elif shape.shapeType == shapefile.POLYLINE:
            draw.line(pixels, fill='black')
    img.show()
Ejemplo n.º 7
0
    def draw_starting_triangle(self, room: DungeonRoom, dungeon: Dungeon,
                               paintableRooms: Dict[DungeonRoom, PaintableRoom],
                               draw: ImageDraw) -> None:
        """
        Internal function for rendering the starting triangle arrow.

        Parameters
        ----------
        room: DungeonRoom
            The room to draw the arrow in.

        draw: ImageDraw
            The drawing handler.
        """

        c = paintableRooms[room].center
        size = 8

        points = []

        next = dungeon.rooms[room.index + 1]
        d = room.direction_to(next)
        if d == 0:
            points.append((c[0] + size, c[1] - size))
            points.append((c[0] + size, c[1] + size))
            points.append((c[0] - size, c[1]))
        if d == 1:
            points.append((c[0] - size, c[1] + size))
            points.append((c[0] + size, c[1] + size))
            points.append((c[0], c[1] - size))
        if d == 2:
            points.append((c[0] - size, c[1] + size))
            points.append((c[0] - size, c[1] - size))
            points.append((c[0] + size, c[1]))
        if d == 3:
            points.append((c[0] + size, c[1] - size))
            points.append((c[0] - size, c[1] - size))
            points.append((c[0], c[1] + size))

        draw.polygon(points, fill=self.pathColor)
Ejemplo n.º 8
0
def draw_block_triangle(canvas: ImageDraw, vertices: FlatTriangle,
                        weights: list[int], master: int) -> None:
    """Draw a flat triangle with (blocks of) lines inside it."""

    weights_0 = [max(weight, 0) for weight in weights]
    sum_weights_0 = sum(weights_0)
    correction = min(min(sum_weights_0 - 2 * e for e in weights_0), 0)
    dual_weights = [
        bigger.half(sum_weights_0 - 2 * e + correction) for e in weights_0
    ]
    parallel_weights = [max(-weight, 0) for weight in weights]
    for i in range(3):
        # Dual arcs.
        if dual_weights[i] > 0:
            # We first do the edge to the left of the vertex.
            # Correction factor to take into account the weight on this edge.
            s_a = (1 - 2 * VERTEX_BUFFER) * weights_0[i - 2] / master
            # The fractions of the distance of the two points on this edge.
            scale_a = (1 - s_a) / 2
            scale_a2 = scale_a + s_a * dual_weights[i] / weights_0[i - 2]

            # Now repeat for the other edge of the triangle.
            s_b = (1 - 2 * VERTEX_BUFFER) * weights_0[i - 1] / master
            scale_b = (1 - s_b) / 2
            scale_b2 = scale_b + s_b * dual_weights[i] / weights_0[i - 1]

            S1 = interpolate(vertices[i - 2], vertices[i - 1], scale_a)
            E1 = interpolate(vertices[i - 0], vertices[i - 1], scale_b)
            S2 = interpolate(vertices[i - 2], vertices[i - 1], scale_a2)
            E2 = interpolate(vertices[i - 0], vertices[i - 1], scale_b2)
            canvas.polygon([S1, E1, E2, S2], fill=LAMINATION_COLOUR)
        elif dual_weights[i] < 0:  # Terminal arc.
            s_0 = (1 - 2 * VERTEX_BUFFER) * weights_0[i] / master

            scale_a = (1 - s_0) / 2 + s_0 * dual_weights[i - 1] / weights_0[i]
            scale_a2 = scale_a + s_0 * (-dual_weights[i]) / weights_0[i]

            S1 = interpolate(vertices[i - 0], vertices[i - 2], scale_a)
            E1 = vertices[i - 1]
            S2 = interpolate(vertices[i - 0], vertices[i - 2], scale_a2)
            E2 = vertices[i - 1]
            canvas.polygon([S1, E1, E2, S2], fill=LAMINATION_COLOUR)
        else:  # dual_weights[i] == 0:  # Nothing to draw.
            pass

        # Parallel arcs.
        if parallel_weights[i]:
            S, O, E = vertices[i - 2], vertices[i - 1], vertices[i]
            SS = interpolate(O, S, VERTEX_BUFFER)
            EE = interpolate(O, E, VERTEX_BUFFER)
            M = interpolate(S, E)
            MM = interpolate(SS, EE)
            s = parallel_weights[i] / master
            P = interpolate(MM, M, s)
            canvas.polygon([S, P, E], fill=LAMINATION_COLOUR)
 def draw_func(draw: ImageDraw):
     for area in areas:
         draw.polygon(
             area.to_img(image.dimensions).as_list(), fill, outline)
 def draw(self, im: ImageDraw):
     im.polygon(self.coordinates, self.color_tuple, self.color_tuple)
Ejemplo n.º 11
0
 def draw(self, draw: ImageDraw):
     draw.polygon(self.points, self.fill, self.outline)
Ejemplo n.º 12
0
 def draw(self, draw: ImageDraw):
     draw.polygon(self.points, self.color)