예제 #1
0
 def stroke_width(self, width):
     width = float(width)
     width = pgmagick.DrawableStrokeWidth(width)
     self.drawer.append(width)
    def render(self, out_path, size=10000, scale=5, bg_color='#0b131a'):
        """
        Render a PNG.
        """

        image = pgm.Image(
            pgm.Geometry(size, size),
            pgm.Color(bg_color),
        )

        # TODO: font

        nodes = self.graph.nodes_iter(data=True)
        count = len(self.graph)

        for tid, n in progress.bar(nodes, expected_size=count):

            # Get X/Y, radius.
            x = (n['x'] * scale) + (size / 2)
            y = -(n['y'] * scale) + (size / 2)
            r = (n['size'] * scale) / 2

            # Index the coordinates.
            self.graph.node[tid]['pixel_x'] = x
            self.graph.node[tid]['pixel_y'] = y
            self.graph.node[tid]['pixel_r'] = r

            # ** Node **

            # Hex-ify color.
            color = '#%02x%02x%02x' % (n['r'], n['g'], n['b'])

            # Draw the node.
            dl = pgm.DrawableList()
            dl.append(pgm.DrawableFillColor(color))
            dl.append(pgm.DrawableStrokeColor('black'))
            dl.append(pgm.DrawableStrokeWidth(r / 15))
            dl.append(pgm.DrawableStrokeOpacity(0.9))
            dl.append(pgm.DrawableCircle(x, y, x + r, y + r))
            image.draw(dl)

            # ** Label **

            label = ', '.join([
                n['label'],
                n['author'],
            ])

            # Measure the width of the label.
            image.fontPointsize(n['size'])
            tm = pgm.TypeMetric()
            image.fontTypeMetrics(label, tm)
            tw = tm.textWidth()

            # Draw the label.
            dl = pgm.DrawableList()
            dl.append(pgm.DrawablePointSize(n['size']))
            dl.append(pgm.DrawableFillColor('white'))
            dl.append(pgm.DrawableText(x - (tw / 2), y, label))
            image.draw(dl)

        image.write(os.path.abspath(out_path))