-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpastecat.py
65 lines (51 loc) · 1.48 KB
/
pastecat.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
#! /usr/bin/env python3
import os
import sys
import argparse
from PIL import ImageGrab, Image
__version__ = '0.0.1'
Image.init()
image_formats = sorted(set(Image.EXTENSION.values()))
parser = argparse.ArgumentParser(
description="Paste clipboard image to file on Windows/macOS."
)
parser.add_argument(
"filename",
nargs="?",
default="-",
help="Output filename. If the filename is `-`, image is written to stdout.",
)
parser.add_argument(
"-f",
dest="format",
nargs="?",
default="",
choices=image_formats,
help="Output file format. Default is PNG. If not specified, format is determined from the filename.",
)
parser.add_argument('--version', action='version', version=f'%(prog)s {__version__}')
def main():
args = parser.parse_args()
format = args.format
if not format:
if args.filename != "-":
ext = os.path.splitext(args.filename)[1].lower()
format = Image.EXTENSION.get(ext)
if not format:
print("Unkown file extension:", ext, file=sys.stderr)
sys.exit(1)
if not format:
format = "PNG"
img = ImageGrab.grabclipboard()
if not img:
print("Clipboard content is not image data.", file=sys.stderr)
sys.exit(1)
if args.filename == "-":
fp = sys.stdout.buffer
else:
fp = open(args.filename, "wb")
img.save(fp, format=format)
if args.filename != "-":
fp.close()
if __name__ == "__main__":
main()