def split_letters(self, path): letters = [0] * 4 bare_name, ext = os.path.splitext(path) if ext == '.gif': from PIL import Image with Image.open(path) as im: path = bare_name + '.png' im.save(path) image = cv2.imread(path, cv2.IMREAD_COLOR) image = self.clear_noise(image) # cv2.imshow("clear", image) # cv2.waitKey(0) splits = [0] * 8 CaptchaUtils.vertical_project(image, splits) if splits[7] < image.shape[ 1]: # assume the final split is in the range of image. for i in range(0, 8, 2): letters[i // 2] = image[0:image.shape[0], splits[i]:splits[i + 1], ].copy() return letters
def clear_noise(self, image): image = cv2.flip(image, -1,) clear_horizontal_noise_line(image) image = cv2.flip(image, -1, ) clear_horizontal_noise_line(image) clear_color(image) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image = cv2.threshold(image, 150, 255, cv2.THRESH_BINARY)[1] CaptchaUtils.clear_peper_noise(image, 2) return image
def split_letters(self, filename, letters): image = cv2.imread(filename, cv2.IMREAD_COLOR) #cv2.imshow("init", image) image = self.clear_noise(image) splits = [0] * 8 CaptchaUtils.vertical_project(image, splits) if splits[7] < image.shape[ 1]: #assume the final split is in the range of image. for i in range(0, 8, 2): letters[i // 2] = image[0:image.shape[0], splits[i]:splits[i + 1], ].copy()