Example #1
0
 def calculate_width(self, font_size, text):
     self.base_image.fontPointsize(font_size)
     tm = TypeMetric()
     self.base_image.fontTypeMetrics(str(text), tm)
     return tm.textWidth()
Example #2
0
 def calculate_caps_height(self, font_size):
     self.base_image.fontPointsize(font_size)
     tm = TypeMetric()
     self.base_image.fontTypeMetrics('X', tm)
     return tm.textHeight()
Example #3
0
    def draw_legend(self):
        if self.hide_legend:
            return
        self.legend_labels = [gdata['label'] for gdata in self.gdata]
        legend_square_width = self.legend_box_size

        dl = DrawableList()

        font = self.font if self.font else DEFAULT_FONT
        dl.append(DrawablePointSize(self.legend_font_size))

        label_widths = [[]]     # Used to calculate line wrap
        for label in self.legend_labels:
            metrics = TypeMetric()
            self.base_image.fontTypeMetrics(str(label), metrics)
            label_width = metrics.textWidth() + legend_square_width * 2.7
            label_widths[-1].append(label_width)
            if sum(label_widths[-1]) > (self.raw_columns * 0.9):
                label_widths.append([label_widths[-1].pop()])

        current_x_offset = self.center(sum(label_widths[0]))
        if self.hide_title:
            current_y_offset = self.top_margin + self.title_margin
        else:
            current_y_offset = self.top_margin + self.title_margin + self.title_caps_height

        dl.append(DrawableStrokeColor('transparent'))
        for index, legend_label in enumerate(self.legend_labels):
            # Now draw box with color of this dataset
            dl.append(DrawableFillColor(Color(self.gdata[index]['color'])))
            dl.append(DrawableRectangle(current_x_offset,
                                        current_y_offset - legend_square_width / 2.0,
                                        current_x_offset + legend_square_width,
                                        current_y_offset + legend_square_width / 2.0))

            # Draw label
            dl.append(DrawableFillColor(Color(self.font_color)))
            font = self.font if self.font else DEFAULT_FONT
            dl.append(DrawableFont(font, StyleType.NormalStyle, 400,
                                   StretchType.NormalStretch))
            dl.append(DrawablePointSize(self.legend_font_size))
            dl.append(DrawableGravity(GravityType.NorthWestGravity))
            x = current_x_offset + legend_square_width * 1.7
            y = current_y_offset + self.legend_caps_height / 3
            dl.append(DrawableText(x, y, str(legend_label)))

            dl.append(DrawablePointSize(self.legend_font_size))
            metrics = TypeMetric()
            self.base_image.fontTypeMetrics(str(legend_label), metrics)
            current_string_offset = metrics.textWidth() + legend_square_width * 2.7

            # Handle wrapping
            del(label_widths[0][0])
            if len(label_widths[0]) == 0:
                del(label_widths[0])
                if len(label_widths) != 0:
                    current_x_offset = self.center(sum(label_widths[0]))
                line_height = max([self.legend_caps_height, legend_square_width]) + self.legend_margin
                if len(label_widths) > 0:
                    # Wrap to next line and shrink available graph dimensions
                    current_y_offset += line_height
                    self.graph_top += line_height
                    self.graph_height = self.graph_bottom - self.graph_top
            else:
                current_x_offset += current_string_offset
        if len(dl):
            dl.append(DrawableScaling(self.scale, self.scale))
            self.base_image.draw(dl)
        self.color_index = 0
	def buildNodeImage(self,node):
		

		self.buildCounterNode+=1

		node['name'] = node['name'].encode("utf-8")


		print "{0:.2f}".format(self.buildCounterNode / (self.buildCounterNodeTotal) * 100)," percent complete of this batch                                         \r",

		scale = self.scaleFactor

		#if node['size'] > 10:

		#cale = 4.75

		#if node['size'] < 900:
		#	scale = 4




		circleHeight = int(float(node['size'])*scale)
		circleWidth = int(float(node['size'])*scale)


		canvasHeight = int(circleHeight *2)
		canvasWidth = int(circleWidth* 2) 


		im = Image(Geometry(10,10), 'transparent')
		fontsize = self.returnFontSize(canvasHeight)
		im.fontPointsize(fontsize)
		tm = TypeMetric()
		im.fontTypeMetrics(node['name'], tm)

		if tm.textWidth() > canvasWidth:
			canvasWidth = int(tm.textWidth()) + 5

		im = Image(Geometry(canvasWidth,canvasHeight), 'transparent')
		im.density("72x72")
		im.magick('RGB')
		im.resolutionUnits(ResolutionType.PixelsPerInchResolution)

		im.strokeAntiAlias(True)

		color = (node['rgb'][0],node['rgb'][1],node['rgb'][2])

		color = self.rgb_to_hex( color )
		im.fillColor(color);

		im.strokeWidth(2);

		if circleWidth <= 20:
			im.strokeColor("transparent");
		else:
			im.strokeColor("black");

		if circleWidth <= 50:
			im.strokeWidth(1);


		circle = DrawableCircle( canvasWidth/2 , canvasHeight/2, (canvasWidth/2) + (circleWidth/2), (canvasHeight/2) + (circleHeight/2))
		im.draw(circle)

		im.fillColor("white");
		im.strokeColor("black");
		im.strokeWidth(1);



		fontsize = self.returnFontSize(canvasHeight)
		im.fontPointsize(fontsize)

		

		tm = TypeMetric()
		im.fontTypeMetrics(node['name'], tm)

		textWidth = tm.textWidth()
		textHeight = tm.textHeight()


		if fontsize <= 30:
			im.strokeColor("transparent")
		

		text = DrawableText((canvasWidth / 2) - (textWidth/2), canvasHeight/2 + 6 , node['name'])
		im.draw(text)
		


		im.write(self.dataCircles + str(node['id']) + '.png')
Example #5
0
    def render(self, path, scale=5, width=10000, min_size=10, max_size=200,
               min_fsize=14, max_fsize=200, bg_color='#003059'):

        """
        Render a PNG from the node coordinates.

        Args:
            path (str): The image path.
            scale (float): Pixels per coordinate unit.
            width (int): The height/width, in pixels.
            min_size (int): The min node size.
            max_size (int): The max node size.
            min_fsize (int): The min font size.
            max_fsize (int): The max font size.
        """

        # Initialize the canvas, set font.
        image = Image(Geometry(width, width), Color(bg_color))

        # Set the label font.
        image.font(config['network']['font'])

        for cn, n in bar(self.graph.nodes_iter(data=True),
                         expected_size=len(self.graph)):

            # Get (x,y) / radius.
            x, y = self.get_xy(cn, scale, width)
            r = (n['viz']['size']*scale) / 2

            # Index the coordinates.
            self.graph.node[cn]['x'] = x
            self.graph.node[cn]['y'] = y
            self.graph.node[cn]['r'] = r

            # Get the node label.
            label = ', '.join([
                n.get('title', ''),
                n.get('author', '')
            ])

            # Get the node color.
            color = '#%02x%02x%02x' % (
                n['viz']['color']['r'],
                n['viz']['color']['g'],
                n['viz']['color']['b']
            )

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

            # Compute the font size.
            ratio = (n['viz']['size']-min_size) / (max_size-min_size)
            fsize = min_fsize + (ratio*(max_fsize-min_fsize))
            image.fontPointsize(fsize)

            # Measure the width of the label.
            tm = TypeMetric()
            image.fontTypeMetrics(label, tm)
            tw = tm.textWidth()

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

        image.write(os.path.abspath(path))
Example #6
0
from pgmagick import Image, Geometry, Color, TypeMetric, \
                     DrawableText, DrawableList, DrawableGravity, GravityType

im = Image(Geometry(600, 600), Color("transparent"))

im.fontPointsize(30)
im.fillColor(Color("#f010f0"))
im.strokeColor(Color("transparent"))
im.font("Vera.ttf")
dl = DrawableList()

dl.append(DrawableGravity(GravityType.CenterGravity))
dl.append(DrawableText(0, 0, "center"))

tm = TypeMetric()
im.fontTypeMetrics("northn", tm)
font_height = tm.textHeight()
dl.append(DrawableGravity(GravityType.NorthGravity))
dl.append(DrawableText(0, font_height / 2., "north"))

dl.append(DrawableGravity(GravityType.WestGravity))
dl.append(DrawableText(0, 0, "west"))

dl.append(DrawableGravity(GravityType.EastGravity))
dl.append(DrawableText(0, 0, "east"))
dl.append(DrawableText(0, 20, "east-long"))

dl.append(DrawableGravity(GravityType.SouthGravity))
dl.append(DrawableText(0, 0, "south"))

dl.append(DrawableGravity(GravityType.NorthWestGravity))