def center_of_mass(grid):
    """Find center of mass of an image."""

    x_center = 0
    y_center = 0
    total_weight = 0

    for y in range(grid.shape[0]):
        for x in range(grid.shape[1]):
            weight = grid[y,x]
            if weight > 0.0:
                x_center += grid[y,x] * x
                y_center += grid[y,x] * y
                total_weight += weight

    if total_weight > 0:
        return (y_center / total_weight, x_center / total_weight)
    else:
        return (grid.shape[0]/2, grid.shape[1]/2)