img_gray = cv2.imread(r"./example.png", 0) # the threshold you want is thr thr = 127 bw = ((img_gray / (thr * 2)) > 0.5).astype(np.uint8) * 255 # INV: ">" -> "<"