def centerrect(im, aspect=1.0):
    """Returns a rect that is centered at the center of the given image and contains the image, with the given aspect ratio"""
    w, h = im.size
    cx, cy = w/2.0, h/2.0
    if w/float(h) < aspect:
        w = h*aspect
    if w/float(h) > aspect:
        h = w/aspect
    assert w >= im.size[0]
    assert h >= im.size[1]
    r = (int(cx-w/2.0), int(cy-h/2.0), int(cx+w/2.0 + 0.5), int(cy+h/2.0 + 0.5))
    #print 'returning rect %s with cen %s on im size %s' % (r, rectcenter(r), im.size)
    return r