how can i horizontal line removal from crop captcha picture in python
13:50 09 Mar 2026

I am trying to get a crop picture without horizontal line and i can not get error in my output but it is not working as i want. How can i horizontal line removal from crop captcha picture in python

Is there someone How can I do that in python?

here is my code :

import cv2
import numpy as np
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
def solve_final_step(path):
    img = cv2.imread(path)
    if img is None: return   
    h_orig, w_orig = img.shape[:2]
    roi = img[h_orig//2-35:h_orig//2+35, w_orig//2-110:w_orig//2+110]
       
    lower_black = np.array([0, 0, 0])
    upper_black = np.array([80, 80, 80]) 
    mask = cv2.inRange(roi, lower_black, upper_black)
        
    resized = cv2.resize(mask, None, fx=6, fy=6, interpolation=cv2.INTER_NEAREST)
     
    v_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 5))
    cleaned = cv2.morphologyEx(resized, cv2.MORPH_OPEN, v_kernel)
    
    ero_kernel = np.ones((2, 2), np.uint8)
    eroded = cv2.erode(cleaned, ero_kernel, iterations=1)
    
    dil_kernel = np.ones((2, 2), np.uint8)
    repaired = cv2.dilate(eroded, dil_kernel, iterations=1)

    cv2.imwrite("debug_clean.png", repaired)
    h_f, w_f = repaired.shape
    part_w = w_f 
    captcha = ""
    whitelist = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

    for i in range(4):
        x_s = i * part_w
        x_e = (i + 1) * part_w
        char = repaired[:, x_s:x_e]
        
        char_clean = cv2.copyMakeBorder(char[5:-5, 5:-5], 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=0)
        
        char_inv = cv2.bitwise_not(char_clean)
        cv2.imwrite(f"char_{i}.png", char_inv)

        config = f'--oem 3 --psm 10 -c tessedit_char_whitelist={whitelist}'
        text = pytesseract.image_to_string(char_inv, config=config).strip()
        
        if text:
            captcha += text[0]
        else:
            config_alt = f'--oem 3 --psm 8 -c tessedit_char_whitelist={whitelist}'
            text_alt = pytesseract.image_to_string(char_inv, config=config_alt).strip()
            captcha += text_alt[0] if text_alt else "?"

    print("\n" + "="*20)
    print(f"solve: {captcha}")
    print("="*20)

solve_final_step("3.jpg")  

orginal

enter image description here

outputenter image description here

python crop captcha