def draw_tree(tree, path='tree.jpg'): w = dt.get_width(tree) * 100 h = dt.get_depth(tree) * 100 + 120 img = Image.new('RGB',(w, h),(255,255,255)) draw = ImageDraw.Draw(img) draw_node(draw, tree, w / 2,20) img.save(path,'JPEG')
def draw_tree(tree, path='tree.jpg'): w = dt.get_width(tree) * 100 h = dt.get_depth(tree) * 100 + 120 img = Image.new('RGB', (w, h), (255, 255, 255)) draw = ImageDraw.Draw(img) draw_node(draw, tree, w / 2, 20) img.save(path, 'JPEG')
def draw_node(draw, tree, x, y): if tree.results == None: # Get the width of each branch w1 = dt.get_width(tree.fb) * 100 w2 = dt.get_width(tree.tb) * 100 # Determine the total space required by this node left = x - (w1 + w2) / 2 right = x + (w1 + w2) / 2 # Draw the condition string draw.text((x - 20, y - 10), str(tree.col) + ':' + str(tree.value),(0,0,0)) # Draw links to the branches draw.line((x, y, left + w1 / 2, y + 100), fill = (255,0,0)) draw.line((x, y, right - w2 / 2, y + 100), fill = (255,0,0)) # Draw the branch nodes draw_node(draw, tree.fb, left + w1 / 2, y + 100) draw_node(draw, tree.tb, right - w2 / 2, y + 100) else: txt = ' \n'.join(['%s:%d'%v for v in tree.results.items()]) draw.text((x - 20, y), txt,(0,0,0))
def draw_node(draw, tree, x, y): if tree.results == None: # Get the width of each branch w1 = dt.get_width(tree.fb) * 100 w2 = dt.get_width(tree.tb) * 100 # Determine the total space required by this node left = x - (w1 + w2) / 2 right = x + (w1 + w2) / 2 # Draw the condition string draw.text((x - 20, y - 10), str(tree.col) + ':' + str(tree.value), (0, 0, 0)) # Draw links to the branches draw.line((x, y, left + w1 / 2, y + 100), fill=(255, 0, 0)) draw.line((x, y, right - w2 / 2, y + 100), fill=(255, 0, 0)) # Draw the branch nodes draw_node(draw, tree.fb, left + w1 / 2, y + 100) draw_node(draw, tree.tb, right - w2 / 2, y + 100) else: txt = ' \n'.join(['%s:%d' % v for v in tree.results.items()]) draw.text((x - 20, y), txt, (0, 0, 0))