def twoValueImage(image, G, background='white'):
    """将图像变黑白

    :param image: Image对象
    :param G: 阈值
    :param background 背景颜色， 默认为白色
    :return: Image对象
    """

    # 转成灰度图
    image = image.convert('L')
    # image.show()
    for y in range(0, image.size[1]):
        for x in range(0, image.size[0]):
            g = image.getpixel((x, y))
            if background == 'white':
                if g > G:
                    image.putpixel((x, y), 255)
                else:
                    image.putpixel((x, y), 0)
            else:
                if g > G:
                    image.putpixel((x, y), 0)
                else:
                    image.putpixel((x, y), 255)

    return image