def avgcolor(imageobj):
    """Get the average color of the PIL.Image object.

    imageobj: the image
    returns a RGB tuple.
    """
    r = 0
    g = 0
    b = 0
    totalcount = 0
    colors = 256
    arr = None
    while True:
        arr = imageobj.getcolors(colors)
        if arr is None:
            colors *= 2
        else:
            break
    for tup in arr:
        r += tup[0] * tup[1][0]
        g += tup[0] * tup[1][1]
        b += tup[0] * tup[1][2]
        totalcount += tup[0]
    r //= totalcount
    g //= totalcount
    b //= totalcount
    return r, g, b
