def __init__(self, filename=None, color=None, *args, **kargs): self.img = None if sys.version_info >= (3, ) and isinstance(filename, (str)): self.img = pgmagick.Image(str(filename)) elif sys.version_info < (3, ) and isinstance(filename, (unicode, str)): self.img = pgmagick.Image(str(filename)) elif isinstance(filename, (list, tuple)): size = filename geometry = pgmagick.Geometry(int(size[0]), int(size[1])) if isinstance(color, (list, tuple)): r, g, b = int(color[0]), int(color[1]), int(color[2]) color = pgmagick.Color(r, g, b) self.img = pgmagick.Image(geometry, color) elif isinstance(color, str): if color.find('gradient') == 0 or color.find('plasma') == 0: self.img = pgmagick.Image(geometry, pgmagick.Color()) self.img.read(color) else: color = pgmagick.Color(color) self.img = pgmagick.Image(geometry, color) else: self.img = pgmagick.Image(geometry, pgmagick.Color()) self.img.write(pgmagick.Blob(), 'MIFF') else: self.img = pgmagick.Image()
def _convert_colorobj(input_obj): if isinstance(input_obj, (list, tuple)): r, g, b = int(input_obj[0]), int(input_obj[1]), int(input_obj[2]) color = pgmagick.Color(r, g, b) elif isinstance(input_obj, str): color = pgmagick.Color(input_obj) else: color = input_obj assert isinstance(color, pgmagick.Color) return color
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))