forked from BoboTiG/python-mss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__main__.py
More file actions
91 lines (80 loc) · 2.55 KB
/
__main__.py
File metadata and controls
91 lines (80 loc) · 2.55 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
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
"""
This is part of the MSS Python's module.
Source: https://github.com/BoboTiG/python-mss
"""
import os.path
import sys
from argparse import ArgumentParser
from typing import TYPE_CHECKING
from . import __version__
from .exception import ScreenShotError
from .factory import mss
from .tools import to_png
if TYPE_CHECKING:
from typing import List, Optional # noqa
def main(args=None):
# type: (Optional[List[str]]) -> int
""" Main logic. """
cli_args = ArgumentParser()
cli_args.add_argument(
"-c",
"--coordinates",
default="",
type=str,
help="the part of the screen to capture: top, left, width, height",
)
cli_args.add_argument(
"-l",
"--level",
default=6,
type=int,
choices=list(range(10)),
help="the PNG compression level",
)
cli_args.add_argument(
"-m", "--monitor", default=0, type=int, help="the monitor to screen shot"
)
cli_args.add_argument(
"-o", "--output", default="monitor-{mon}.png", help="the output file name"
)
cli_args.add_argument(
"-q",
"--quiet",
default=False,
action="store_true",
help="do not print created files",
)
cli_args.add_argument("-v", "--version", action="version", version=__version__)
options = cli_args.parse_args(args)
kwargs = {"mon": options.monitor, "output": options.output}
if options.coordinates:
try:
top, left, width, height = options.coordinates.split(",")
except ValueError:
print("Coordinates syntax: top, left, width, height")
return 2
kwargs["mon"] = {
"top": int(top),
"left": int(left),
"width": int(width),
"height": int(height),
}
if options.output == "monitor-{mon}.png":
kwargs["output"] = "sct-{top}x{left}_{width}x{height}.png"
try:
with mss() as sct:
if options.coordinates:
output = kwargs["output"].format(**kwargs["mon"])
sct_img = sct.grab(kwargs["mon"])
to_png(sct_img.rgb, sct_img.size, level=options.level, output=output)
if not options.quiet:
print(os.path.realpath(output))
else:
for file_name in sct.save(**kwargs):
if not options.quiet:
print(os.path.realpath(file_name))
return 0
except ScreenShotError:
return 1
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))