-
Notifications
You must be signed in to change notification settings - Fork 1
/
crop.py
116 lines (96 loc) · 3.4 KB
/
crop.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import click
from click.termui import prompt
import cv2
from helperfuncs import getchars
from crop_utils import autocrop, manual_crop, transform
@click.group()
def main():
pass
@main.command()
@click.option('--src',
help='Image path',
prompt='Image path',
type=str,
required=True)
@click.option('--height',
help='Desired image height in pixels',
type=int,
prompt='Desired image height in pixels',
required=False,
default=1000)
def transform_image(src, height):
image = cv2.imread(src)
resized = transform(image, height)
cv2.namedWindow('image')
cv2.imshow('image', resized)
wait_time = 1000
prop = cv2.getWindowProperty('image', cv2.WND_PROP_VISIBLE)
while prop >= 1:
keyCode = cv2.waitKey(wait_time)
#if (keyCode & 0xFF) == ord("q"):
if cv2.waitKey(0):
cv2.destroyAllWindows()
cont = click.prompt(
'[c] Continue and save or [r] rescale the image again?',
type=click.Choice(['c', 'r']))
if cont == 'r':
new_height = click.prompt('Height in pixels', type=int)
resized = transform(image, new_height)
cv2.namedWindow('resized')
cv2.imshow('resized', resized)
else:
break
save_img = click.prompt('Save the image?', type=click.Choice(['y', 'n']))
if save_img == 'y':
valid_path = False
dst_dir = click.prompt('Enter the destination directory', type=str)
while not valid_path:
try:
cv2.imwrite(dst_dir, resized)
valid_path = True
except cv2.error:
click.echo('Invalid directory path. Try again...')
dst_dir = click.prompt('Enter the destination directory',
type=str)
click.echo(f'Saved to: {dst_dir}')
click.echo('Done')
else:
click.echo('Done')
@main.command()
@click.option('--dst',
type=str,
prompt='Path to destination folder',
default='manual_crop')
@click.option('--img',
type=str,
prompt='Path to image to crop the characters from')
@click.option('--vars',
type=int,
prompt='Number of variations for each of your characters')
def crop_manual(dst, img, vars):
image = cv2.imread(img)
custom_punc = click.prompt(
"Define the puncutation characters you'll be using", type=str)
# with open('txtsplit/punctuation_all.txt', 'w') as f:
# f.write(custom_punc)
uppercase = getchars('uppercase.txt')
lowercase = getchars('alllowercase.txt')
punctuation = [c for c in custom_punc]
chars_to_crop = lowercase + uppercase + punctuation
manual_crop(dst, image, vars, chars_to_crop)
@main.command()
@click.option('--src',
type=str,
prompt='Path to source folder of images you cropped manually',
required=True,
default='manual_crop')
@click.option('--dst',
type=str,
prompt='Path to destination folder',
required=True,
default='autocropped')
def crop_auto(src, dst):
autocrop(src, dst)
click.echo(f'Done: autocropped images from {src} to {dst}')
if __name__ == "__main__":
main()