def getBackgroundColor(im):
    """
    Detect the background color of an image.
    The color is assumed to be the the most common
    pixel color along the border of the image.
    """
    borderPixels = []
    borderColors = []
    for x in range(im.size[0]):
        # Get pixel along top
        px = im.getpixel((x, 0))[:3]
        borderPixels.append(px)
        if px not in borderColors:
            borderColors.append(px)

        # Get pixel along bottom
        px = im.getpixel((x, im.size[1] - 1))[:3]
        borderPixels.append(px)
        if px not in borderColors:
            borderColors.append(px)

    for y in range(1, im.size[1] - 1):
        # Get pixel along left
        px = im.getpixel((1, y))[:3]
        borderPixels.append(px)
        if px not in borderColors:
            borderColors.append(px)

        # Get pixel along right
        px = im.getpixel((im.size[0] - 1 - 1, y))[:3]
        borderPixels.append(px)
        if px not in borderColors:
            borderColors.append(px)

    mostOccurrences = 0
    mostCommonColor = None
    for color in borderColors:
        numOccurrences = borderPixels.count(color)
        if numOccurrences > mostOccurrences:
            mostOccurrences = numOccurrences
            mostCommonColor = color

    return mostCommonColor