def trim_image(img, white_bg=True, thresh=240):
    """
    Trim edges of image.

    Args:
        img (numpy.array): 2D rectangular grayscale image
    """
    if not white_bg:
        thresh = 255 - thresh
    top_found = False
    bot_found = False
    left_found = False
    right_found = False
    top_ind = img.shape[1]
    bot_ind = 0
    left_ind = img.shape[0]
    right_ind = 0
    for r, row in enumerate(img):
        for c, pix in enumerate(row):
            if pix <= thresh:
                top_ind = min(top_ind, r)
                bot_ind = max(bot_ind, r)
                left_ind = min(left_ind, c)
                right_ind = max(right_ind, c)
    return top_ind, bot_ind, left_ind, right_ind
