def drawDendrogram(cluster, file="dendrogram.jpg"):
    """
    Given a cluster, outputs a dendrogram depiction of that cluster as
    a jpg image.  The lengths of the lines depicting a pair of
    children are proportional to the degree of dissimilarity between
    the pair.
    """

    imageHeight = getWidth(cluster) * 20
    imageWidth = 1200

    clusterDepth = getDepth(cluster)

    scaling = float(imageWidth-150)/clusterDepth

    # Create a new image with a white background
    img = Image.new('RGB',(imageWidth,imageHeight), (255,255,255))
    draw = ImageDraw.Draw(img)

    # Draw the dendrogram
    draw.line((0,imageHeight/2,10,imageHeight/2), fill=(0,0,0))
    drawNode(draw, cluster, 10, imageHeight/2, scaling)

    # Save the image
    img.save(file,'JPEG')
def drawNode(draw, cluster, x, y, scaling):
    """
    Given a DrawImage 'draw', and cluster 'cluster', draw the
    dendrogram representing the cluster with its root located at x,y.
    """
    if cluster.left == None and cluster.right == None:
        # Draw a leaf node (i.e. just print the label)
        draw.text((x+5,y-7),cluster.name,(0,0,0))
    else:
        # Draw a branch node
        h1 = getWidth(cluster.left) * 20
        h2 = getWidth(cluster.right) * 20
        branchLength = cluster.error * scaling
        y0 = y - h2/2
        y1 = y + h1/2

        draw.line((x,y0,x,y1), fill=(0,0,0))
        draw.line((x,y0,x+branchLength,y0), fill=(0,0,0))
        draw.line((x,y1,x+branchLength,y1), fill=(0,0,0))
        drawNode(draw, cluster.left, x+branchLength, y0, scaling)
        drawNode(draw, cluster.right, x+branchLength, y1, scaling)