def emotion_to_avatar(emotion, font_path, color=Color("black")): """使用颜文字(文本)来生成一张方形图片 :type emotion: string :param emotion: 需要绘制在图像上的文本 :type font_path: string :param font_path: 字体文件路径 :type color: Color :param color: 绘制的文本颜色 返回结果是一个 Image 对象,并叫将会被规整到 AVATAR_SIZE 设定的大小 """ font_size = 128 max_size = len(emotion.decode('utf-8')) * font_size img = Image(Geometry(max_size, max_size), Color("white")) img.font(font_path) img.fontPointsize(font_size) img.annotate(emotion, GravityType.CenterGravity) img.trim() img.write('tmp.png') height = img.rows() width = img.columns() origin_pimg = PImage.open('tmp.png') new_pimg = PImage.new("RGB", (max(height, width), max(height, width)), "white") if height > width: new_pimg.paste(origin_pimg, ((height - width) / 2, 0)) else: new_pimg.paste(origin_pimg, (0, (width - height) / 2)) return new_pimg.resize(AVATAR_SIZE, PImage.ANTIALIAS)
from pgmagick import Image, Geometry, Color, \ DrawableCircle, DrawableText im = Image(Geometry(300, 300), Color("yellow")) circle = DrawableCircle(100.0, 100.0, 20.0, 20.0) im.draw(circle) im.fontPointsize(65) im.font("/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType/UnBatang.ttf") text = DrawableText(30, 250, "hello gm") im.draw(text) im.write('circle-text.png')
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))
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))