-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsm.py
143 lines (126 loc) · 6.09 KB
/
dsm.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import cv2
import numpy as np
import argparse
from os.path import exists
parser = argparse.ArgumentParser(description='Make Digital Signature')
parser.add_argument('PATH',
metavar='path_to_image',
type=str,
help='the path to the signature image')
parser.add_argument('-a',
'--auto',
action='store_true',
help='make signature with default settings and save it')
parser.add_argument('-c',
'--color',
dest='COLOR',
type=str,
default='',
help='Set the color of the Signature, r - red, g - green, b - blue or rgb for white or any other combination.')
args = parser.parse_args()
PATH_TO_SIGNATURE = args.PATH
if not exists(PATH_TO_SIGNATURE):
print('The path specified does not exist')
quit()
def make_sig(signature_org, lower_threshold, blur_amount, auto=0):
if auto: # cv2.THRESH_BINARY = 0
auto = cv2.THRESH_OTSU # cv2.THRESH_OTSU = 8
lower_threshold = 0
split_img = cv2.split(signature_org)
binary_sig = np.full_like(split_img[0], 255)
for color_channel in split_img[:3]:
binary_sig = cv2.bitwise_and(binary_sig, cv2.threshold(color_channel, lower_threshold, 255, auto)[1])
if blur_amount > 0:
blur_amount = 2*blur_amount + 1 # (2*n + 1)
blur_binary_sig = cv2.medianBlur(binary_sig, blur_amount)
else:
blur_binary_sig = binary_sig
trans_sig = cv2.cvtColor(blur_binary_sig, cv2.COLOR_GRAY2BGRA)
alpha_channel = 255 - blur_binary_sig
blur_binary_sig = trans_sig.copy()
trans_sig[:, :, 3] = alpha_channel
color_map = {'r' : 2, 'g' : 1, 'b': 0}
for color in args.COLOR.lower():
trans_sig[:, :, color_map[color]], blur_binary_sig[:, :, color_map[color]] = 255, 255
return blur_binary_sig, trans_sig
def click_and_crop(event, x, y, flags, param):
# grab references to the global variables
global refPt, cropping, signature_org
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being performed
if event == cv2.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
cropping = True
# check to see if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
# record the ending (x, y) coordinates and indicate that
# the cropping operation is finished
refPt.append((x, y))
cropping = False
# draw a rectangle around the region of interest
cv2.rectangle(signature_org, refPt[0], refPt[1], 0, 2)
cv2.imshow('Original Image', signature_org)
def make_show_sig(sig_file_name, lower_threshold=127, blur_amount=2, height=300, width=800):
sig_name = sig_file_name.split('.')[0]
global signature_org
signature_org = cv2.imread(sig_file_name)
if args.auto:
binary_sig , trans_sig = make_sig(signature_org, lower_threshold, blur_amount, auto=1)
cv2.imwrite(sig_name + '_binary.png', binary_sig)
cv2.imwrite(sig_name + '_trans.png', trans_sig)
return 0
should_crop = input("\nWhat to crop the image before making signature ? [y/ any]:- ").lower()
if should_crop == 'y':
global refPt, cropping
refPt = []
cropping = False
clone_signature = signature_org.copy()
cv2.namedWindow('Original Image')
cv2.imshow('Original Image', signature_org)
cv2.setMouseCallback('Original Image', click_and_crop)
print('Usage: press r to reset the crop area\n\t '
'press c to crop (if you dont want to crop you can press c without selecting crop area or press x button)\n'
'Due to Open cv limitaions larger images are not displayed correctly. In that case you may need to crop image on other software')
while cv2.getWindowProperty('Original Image', cv2.WND_PROP_VISIBLE):
cv2.imshow('Original Image', signature_org)
key = cv2.waitKey(1) & 0xFF
# if the 'r' key is pressed, reset the cropping region
if key == ord("r"):
signature_org = clone_signature.copy()
# if the 'c' key is pressed, break from the loop
elif key == ord("c"):
break
if len(refPt) == 2:
refPt = np.array(refPt)
[y,y1], [x,x1] = [refPt[:, 1], refPt[:, 0]]
signature_org = clone_signature[y:y1, x:x1]
else:
signature_org = clone_signature
cv2.destroyAllWindows()
prev_l_t, prev_b_a = (0, 0)
cv2.namedWindow('Your Digital Signature', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Your Digital Signature', width, height)
cv2.createTrackbar('lower_threshold','Your Digital Signature',0,255, lambda x: None)
cv2.createTrackbar('blur_amount','Your Digital Signature',0,25, lambda x: None)
cv2.setTrackbarPos('lower_threshold','Your Digital Signature', lower_threshold)
cv2.setTrackbarPos('blur_amount','Your Digital Signature', blur_amount)
while cv2.getWindowProperty('Your Digital Signature', cv2.WND_PROP_VISIBLE):
l_t = cv2.getTrackbarPos('lower_threshold','Your Digital Signature')
b_a = cv2.getTrackbarPos('blur_amount','Your Digital Signature')
if (l_t != prev_l_t) or (b_a != prev_b_a):
binary_sig , trans_sig = make_sig(signature_org, l_t, b_a)
prev_l_t = l_t
prev_b_a = b_a
cv2.imshow("Your Digital Signature", trans_sig)
key = cv2.waitKey(1) & 0xFF
if key == ord('r'):
cv2.setTrackbarPos('lower_threshold','Your Digital Signature', lower_threshold)
cv2.setTrackbarPos('blur_amount','Your Digital Signature', blur_amount)
if key == ord('s'):
cv2.imwrite(sig_name + '_binary.png', binary_sig)
cv2.imwrite(sig_name + '_trans.png', trans_sig)
break
elif key == ord('q'):
break
cv2.destroyAllWindows()
make_show_sig(PATH_TO_SIGNATURE)