<reponame>samorojy/captcha_solver
from PIL import Image


def cut_letters(image_file_name):
    image = Image.open(image_file_name)
    image = image.convert("RGB")

    clear_noise(image)
    letters_list = []
    for i in range(0, 4):
        box = (i * 30, 0, (i * 30 + 30), 40)
        letters_list.append(image.crop(box))
    return letters_list


# Clean the background noise, if color != white, then set to black.
def clear_noise(image, threshold=200):
    pixel_data = image.load()
    for y_coordinate in range(image.size[1]):
        for x_coordinate in range(image.size[0]):
            if (pixel_data[x_coordinate, y_coordinate][0] > threshold) \
                    and (pixel_data[x_coordinate, y_coordinate][1] > threshold) \
                    and (pixel_data[x_coordinate, y_coordinate][2] > threshold):

                pixel_data[x_coordinate, y_coordinate] = (255, 255, 255)
            else:
                pixel_data[x_coordinate, y_coordinate] = (0, 0, 0, 255)

from PIL import Image


def cut_letters(image_file_name):
    image = Image.open(image_file_name)
    image = image.convert("RGB")

    clear_noise(image)
    letters_list = []
    for i in range(0, 4):
        box = (i * 30, 0, (i * 30 + 30), 40)
        letters_list.append(image.crop(box))
    return letters_list


# Clean the background noise, if color != white, then set to black.
def clear_noise(image, threshold=200):
    pixel_data = image.load()
    for y_coordinate in range(image.size[1]):
        for x_coordinate in range(image.size[0]):
            if (pixel_data[x_coordinate, y_coordinate][0] > threshold) \
                    and (pixel_data[x_coordinate, y_coordinate][1] > threshold) \
                    and (pixel_data[x_coordinate, y_coordinate][2] > threshold):

                pixel_data[x_coordinate, y_coordinate] = (255, 255, 255)
            else:
                pixel_data[x_coordinate, y_coordinate] = (0, 0, 0, 255)
# Clean the background noise, if color != white, then set to black.