forked from stoic1979/robovision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencv.py
78 lines (55 loc) · 2.22 KB
/
opencv.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
#!/usr/bin/python3
"""
RoboVision
______________
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Project Author/Architect: Navjot Singh <[email protected]>
"""
#
# command line script for opencv functions
#
import argparse
import sys
import cv2
class Opencv(object):
def __init__(self):
parser = argparse.ArgumentParser(description='Opencv commandline')
parser.add_argument('command', help='Subcommand to run')
args = parser.parse_args(sys.argv[1:2])
if not hasattr(self, args.command):
print("Unrecognized command")
parser.print_help()
exit(1)
# use dispatch pattern to invoke method with same name
getattr(self, args.command)()
def to_grayscale(self):
try:
parser = argparse.ArgumentParser(
description='Convert an image to grayscale')
# required fields for creating a gitlab user
parser.add_argument('--input', help='input image name/path')
parser.add_argument('--output', help='output image name/path')
# parse args for this command
args = parser.parse_args(sys.argv[2:])
# converting image to grayscale
img = cv2.imread(args.input, 0)
# saving grayscaled image to output file
cv2.imwrite(args.output, img)
print(
"Converted '%s' to grayscale and saved in '%s'" %
(args.input, args.output))
except Exception as exp:
print(
"Got exception while converting image to grayscale: ",
str(exp))
if __name__ == "__main__":
Opencv()