def dummy_image(self, width, height): im = Image(Geometry(width, height), Color(240, 240, 240)) im.strokeColor(Color(128, 128, 128)) im.strokeWidth(1) im.draw(DrawableLine(0, 0, width, height)) im.draw(DrawableLine(0, height, width, 0)) return im
def dummy_image(self, width, height): d = self._get_dummy_image_data(width, height) im = Image(Geometry(width, height), Color(*d['canvas_color'])) im.strokeColor(Color(*d['line_color'])) im.strokeWidth(1) for line in d['lines']: im.draw(DrawableLine(*line)) im.fillColor(Color()) im.draw(DrawableRectangle(*d['rectangle'])) return im
#make a world-generator, bitmap based. from pgmagick import Image, DrawableCircle, DrawableText, Geometry, Color im = Image(Geometry(300, 300), Color("yellow")) circle = DrawableCircle(100, 100, 20, 20) im.draw(circle) im.fontPointsize(65) text = DrawableText(30, 250, "Hello pgmagick") im.draw(text) im.write('test.png')
def createCard(ork): template = Image('templates/cardtemplate.png') name = ork.name abilities = {} # Name template.fontPointsize(50) nameString = name if (ork.specialist != None): nameString += ", " + ork.specialist template.draw(DrawableText(38, 84, nameString)) # Stats template.fontPointsize(45) template.draw(DrawableText(52, 185, ork.stats['M'])) template.draw(DrawableText(135, 185, ork.stats['WS'])) template.draw(DrawableText(230, 185, ork.stats['BS'])) template.draw(DrawableText(330, 185, ork.stats['S'])) template.draw(DrawableText(416, 185, ork.stats['T'])) template.draw(DrawableText(485, 185, ork.stats['A'])) template.draw(DrawableText(566, 185, ork.stats['W'])) template.draw(DrawableText(662, 185, ork.stats['Ld'])) template.draw(DrawableText(783, 185, ork.stats['Sv'])) # Weapons template.fontPointsize(30) weaponOffset = 40 weaponY = 277 for weapon in ork.weapons: weaponName = weapon['name'] if (weaponName.startswith('Kombi-weapon with')): start = weaponName.find('(') + 1 end = weaponName.find(')') weaponName = weaponName[start:end] weaponNameStrings = wrap(weaponName, 14) for i in range(min(len(weaponNameStrings), 2)): template.draw(DrawableText(43, weaponY, weaponNameStrings[i])) if (i + 1 < len(weaponNameStrings)): weaponY += 35 template.draw(DrawableText(295, weaponY, weapon['stats']['Range'])) template.draw(DrawableText(445, weaponY, weapon['stats']['Type'])) if (weapon['stats']['S'] == 'User'): template.draw(DrawableText(656, weaponY, 'U')) else: template.draw(DrawableText(656, weaponY, weapon['stats']['S'])) template.draw(DrawableText(713, weaponY, weapon['stats']['AP'])) template.draw(DrawableText(777, weaponY, weapon['stats']['D'])) if (weapon['stats']['Abilities'] != '_'): abilities[weaponName] = weapon['stats']['Abilities'] template.draw(DrawableText(846, weaponY, 'Yes')) weaponY += weaponOffset # Wargear wargearNames = [] for wargear in ork.wargear: wargearName = wargear['name'] wargearNames.append(wargearName) if (wargear['stats']['Ability'] != '_'): abilities[wargearName] = wargear['stats']['Ability'] wargearString = "Wargear: " + ", ".join(wargearNames) wargearStrings = wrap(wargearString, 50) # Only two lines for wargear. wargearY = 522 wargearOffset = 35 for i in range(min(len(wargearStrings), 2)): template.draw(DrawableText(39, wargearY, wargearStrings[i])) wargearY += wargearOffset # Abilities abilityNames = [] if (ork.subFaction != None): abilityName = ork.subFaction['name'] abilityNames.append(abilityName) abilities[abilityName] = ork.subFaction['Description'] for ability in ork.abilities: abilityName = ability['name'] abilityNames.append(abilityName) abilities[abilityName] = ability['stats']['Description'] abilityString = "Abilities: " + ", ".join(abilityNames) abilityStrings = wrap(abilityString, 50) # Only four lines for abilities. abilityY = 595 abilityOffset = 35 for i in range(min(len(abilityStrings), 4)): template.draw(DrawableText(39, abilityY, abilityStrings[i])) abilityY += abilityOffset template.write(name + '.png') template = Image('templates/cardtemplate-back.png') template.fontPointsize(30) # Backside of the card abilityY = 117 abilityOffset = 35 # Abilities, also from weapons for abilityName in abilities.keys(): abilityStrings = wrap(abilityName + ": " + abilities[abilityName], 60) for string in abilityStrings: template.draw(DrawableText(39, abilityY, string)) abilityY += abilityOffset abilityY += 10 template.write(name + '-back.png')
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 edgeTest(self): #first build the image, we need to know how big it is with open(self.dataBase + "base.json", "r") as f: base = json.loads(f.read()) self.totalWidth = base['width'] self.totalHeight = base['height'] print ("Creating large base image", int(self.totalWidth), 'x',int(self.totalHeight) ) #im = Image(Geometry(int(self.totalWidth), int(self.totalHeight)), Color("black")) im = Image(Geometry(5000, 5000), Color("black")) allNodes = {} for file in os.listdir(self.dataNodes): if file.endswith('.json'): with open(self.dataNodes + file, "r") as f: nodes = json.loads(f.read()) print ("Storing Nodes data", self.dataNodes + file, len(nodes)) self.buildCounterNodeTotal = len(nodes) self.buildCounterNode = 0 for node in nodes: allNodes[node['id']] = node totalEdges = 0 for file in os.listdir(self.dataEdges): if file.endswith('.json'): with open(self.dataEdges + file, "r") as f: edges = json.loads(f.read()) print ("Building Image Edges", self.dataEdges + file, len(edges)) self.buildCounterNodeTotal = len(edges) self.buildCounterNode = 0 drawlist = DrawableList() for edge in edges: sourcePos = allNodes[edge['source']]['posX'], allNodes[edge['source']]['posY'] targetPos = allNodes[edge['target']]['posX'], allNodes[edge['target']]['posY'] width = abs(sourcePos[0]-targetPos[0]) height = abs(sourcePos[1]-targetPos[1]) dx = targetPos[0] - sourcePos[0] dy = targetPos[1] - sourcePos[1] dxdy = (dx*dx) + (dy*dy) dist = math.sqrt( dxdy ) dxdy = (dx*dx) + (dy*dy) dist = math.sqrt( dxdy ) #midpoint mx = (targetPos[0] + sourcePos[0]) / 2 my = (targetPos[1] + sourcePos[1]) / 2 #print width, height, dist totalEdges+=1 color = (allNodes[edge['source']]['rgb'][0],allNodes[edge['source']]['rgb'][1],allNodes[edge['source']]['rgb'][2]) color = self.rgb_to_hex( color ) drawlist.append(DrawableStrokeColor(color)) drawlist.append(DrawableStrokeOpacity(0.25)) drawlist.append(DrawableLine(0,height,width,0)) #line = Image(Geometry(int(width), int(height)), Color("black")) #line.strokeColor("blue"); #line.draw(drawlist) cords = self.convertCoordinates(int(mx),int(my)) print str(totalEdges), " \r", sys.stdout.flush() #line.write(str(edge['id']) + 'line.png') #im.composite(line, int(cords[0]), int(cords[1]), co.OverCompositeOp) if totalEdges > 1000: im.draw(drawlist) print ("") print ("Writing large file out") im.write('base.png') sys.exit() print totalEdges sys.exit()
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')
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, \ DrawableAffine, DrawableCircle, DrawableCompositeImage, \ DrawableText, DrawableList im = Image(Geometry(300, 300), Color("yellow")) circle = DrawableCircle(100.0, 100.0, 20.0, 20.0) im.fontPointsize(65) im.font("/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType/UnBatang.ttf") text = DrawableText(30, 250, "hello gm") drawlist = DrawableList() drawlist.append(circle) drawlist.append(text) im.draw(drawlist) im.write('non-affine.png') im = Image(Geometry(300, 300), Color("yellow")) circle = DrawableCircle(100.0, 100.0, 20.0, 20.0) im.fontPointsize(65) im.font("/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType/UnBatang.ttf") text = DrawableText(30, 250, "hello gm") drawlist = DrawableList() drawlist.append(DrawableAffine(0.9, 0.9, 0.1, 0.1, 0.5, 0.5)) drawlist.append(circle) drawlist.append(text) im.draw(drawlist) im.write('affine.png') im = Image(Geometry(300, 300), Color("yellow")) d = DrawableCompositeImage(100, 150, im) im.draw(d)
from pgmagick import Image, Geometry, Color, Coordinate, CoordinateList, \ DrawableBezier im = Image(Geometry(300, 200), Color("white")) cdl = CoordinateList() cdl.append(Coordinate(20, 20)) cdl.append(Coordinate(100, 50)) cdl.append(Coordinate(50, 100)) cdl.append(Coordinate(160, 160)) db = DrawableBezier(cdl) im.draw(db) im.display()
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)) dl.append(DrawableText(0, font_height / 2., "north-west")) dl.append(DrawableGravity(GravityType.NorthEastGravity)) dl.append(DrawableText(0, font_height / 2., "north-east")) dl.append(DrawableGravity(GravityType.SouthWestGravity)) dl.append(DrawableText(0, 0, "south-west")) dl.append(DrawableGravity(GravityType.SouthEastGravity)) dl.append(DrawableText(0, 0, "south-east")) im.draw(dl) im.write("test.png")
line_inputs = list(filter(None, panel.split('\n'))) for line_input in line_inputs: split_line_input = line_input.split(':') actor = split_line_input[0] line = split_line_input[1] if "," in actor: split_actor = actor.split(",") actor = split_actor[0] attitude = split_actor[1] else: attitude = "neutral" lines.append({ "actor": actor.strip(), "attitude": attitude.strip(), "line": line.strip() }) panels.append(lines) num = 0 for panel in panels: bg = Image('backgrounds/{}.png'.format(background)) line_num = 1 for line in panel: actor = Image('sprites/{}/{}.png'.format(line['actor'], line['attitude'])) draw_line = DrawableText(20 * line_num, 95, line['line']) bg.composite(actor, 20 * line_num, 100, co.OverCompositeOp) bg.draw(draw_line) line_num = line_num + 4 bg.write('output{}.png'.format(num)) num = num + 1