def clean(img, linePointThresold = 5): for h in range(1, img.height - 1): for w in range(1, img.width - 1): c = img.getpixel((w, h)) if color.isBackground(c): img.putpixel((w, h), color.BLACK_COLOR) else: similars = ImageUtils.getSimilarNeighbors(img, c, (w, h), [(0,0)]) similarCount = len(similars) if 0 == similarCount: if len(ImageUtils.getBackgrondNeighbors(img, c, (w, h))) == 7: img.putpixel((w, h), color.BLACK_COLOR) continue count = 1 if 1 == similarCount: ww, hh = similars[0] if TraverseLine(img, w + ww, h + hh, (-ww, -hh), linePointThresold, similars, count): img.putpixel((w, h), color.BLACK_COLOR) elif 2 == similarCount: ww, hh = similars[0] ww1, hh1 = similars[1] if (ww * ww1 + hh * hh1) >= 0: img.putpixel((w, h), color.BLACK_COLOR) continue if (1, 0) not in similars or (-1, 1) not in similars: if hh1 > hh or (hh1 == hh and ww1 > ww): tmpW = ww; tmpH = hh ww = ww1; hh = hh1 ww1 = tmpW; hh1 = tmpH if TraverseLine(img, w + ww, h + hh, (-ww, -hh), linePointThresold, similars, count): img.putpixel((w, h), color.BLACK_COLOR) img.putpixel((w + ww1, h + hh1), color.BLACK_COLOR) continue from functools import partial continueDo = partial(TraverseLine, img, w + ww1, h + hh1, (-ww1, -hh1), linePointThresold) if TraverseLine(img, w + ww, h + hh, (-ww, -hh), linePointThresold, [similars[0], (-ww1, -hh1)], count, continueDo): img.putpixel((w, h), color.BLACK_COLOR)