首页 > 解决方案 > 使用 Python 清除验证码图像的线条噪声

问题描述

在此处输入图像描述

我试图删除这个验证码图像上的线条,所以我可以使用像 tesseract 这样的 OCR 工具来读取图像上的字符串文本。我使用的代码在我如何为我的大学网站和图像增强开发验证码破解程序中进行了解释,以便更好地识别图像。到目前为止,这是我的 python 代码(实际上我是 python 新手)

from PIL import Image, ImageEnhance


im = Image.open("img2.png")
nx, ny = im.size
image = im.resize((int(nx*5), int(ny*5)), Image.BICUBIC)
image.save("img1_enchance.png")

image = image.convert("L") # Grayscale conversion
width, height = image.size
cropped_image = image.crop((0, 0, (460/3), 200))
cropped_image.save("img1_crop.png")

pixel_matrix = cropped_image.load()
croppedwidth, croppedheight = cropped_image.size
for col in range(0, croppedheight): # Height
    for row in range(0, croppedwidth): # Width
        if pixel_matrix[row, col] != 0:
            pixel_matrix[row, col] = 255
cropped_image.save("img1_text1.png")

for column in range(1, croppedheight - 1):
    for row in range(1, croppedwidth - 1):
        if pixel_matrix[row, column] == 0 \
            and pixel_matrix[row, column - 1] == 255 and pixel_matrix[row, column + 1] == 255:
            pixel_matrix[row, column] = 255
        if pixel_matrix[row, column] == 0 \
            and pixel_matrix[row - 1, column] == 255 and pixel_matrix[row + 1, column] == 255:
            pixel_matrix[row, column] = 255
cropped_image.save("img1_text2.png")

问题是,得到了文本字符串,我得到了如下图所示的嘈杂线:

(img1_text1.png)

img1_text1.png

(img1_text2.png)

img1_text2.png

我在这个google-drive 链接中收集了如下所示的验证码图像:

在此处输入图像描述

非常感谢任何帮助,非常感谢

标签: pythoncaptchacv2

解决方案


推荐阅读