-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcut.py
More file actions
32 lines (30 loc) · 1.14 KB
/
cut.py
File metadata and controls
32 lines (30 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from os import makedirs
import os.path
from glob import glob
from functions import *
from constants import *
image_files = glob(os.path.join(CAPTCHA_FOLDER, '*.png'))
for image_file in image_files:
print('Now doing file:', image_file)
answer = os.path.basename(image_file).split('_')[0]
image = cv2.imread(image_file)
processed = process_image(image)
contours = get_contours(processed)
if not len(contours):
print('[!] Could not extract contours')
continue
letters = get_letters(processed, contours)
if len(letters) != NR_CHARACTERS:
print('[!] Could not extract desired amount of characters')
continue
if any([l.shape[0] < 10 or l.shape[1] < 10 for l in letters]):
print('[!] Some of the extracted characters are too small')
continue
for i, mask in enumerate(letters):
letter = answer[i]
outfile = '{}_{}.png'.format(answer, i)
outpath = os.path.join(LETTERS_FOLDER, letter)
if not os.path.exists(outpath):
makedirs(outpath)
print('[i] Saving', letter, 'as', outfile)
cv2.imwrite(os.path.join(outpath, outfile), mask)