-
Notifications
You must be signed in to change notification settings - Fork 6
/
pasteimg
executable file
·70 lines (51 loc) · 1.91 KB
/
pasteimg
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
#!/usr/bin/env python
import pygtk
import gtk
import os
import sys
import random
import argparse
class PasteImg:
# Removed 'ico' from list until can catch size error
imgTypeSet = ['png', 'bmp', 'jpg', 'jpeg', 'tiff']
def __init__(self):
parser = argparse.ArgumentParser(description='PasteImg: Output clipboard to file')
parser.add_argument('filename', nargs='?', default='',
help='filename to use')
parser.add_argument('-v','--version', action='version', version='%(prog)s 0.2')
args = parser.parse_args()
filename = args.filename
clipb = gtk.clipboard_get()
clipb.request_image(self.callback_img, filename)
def callback_img(self, clipboard, pixbuf, filename):
if filename == '':
filename=''.join(random.choice('0123456789abcdef') for i in range(12))
# Split filename suffix
(shortname, extension) = os.path.splitext(filename)
fileExt=extension.replace('.','')
# Default to png
imgType="png"
if fileExt in self.imgTypeSet:
# pixbuf only recongnises 'jpeg' as type
if fileExt == 'jpg': fileExt = 'jpeg'
imgType=fileExt
elif fileExt == "":
fileExt=imgType
else:
# Wrong extension
print "Error: Unknown file extension '%s'" % extension
sys.exit()
# Save clipboard image to file
if pixbuf is None:
print 'No image available!'
else:
print 'Saving clipboard image to file(%s): %s.%s' % (imgType, shortname, fileExt)
pixbuf.save(filename, imgType)
self.exitPasteImg()
def exitPasteImg(self):
gtk.main_quit()
def main(self):
gtk.main()
if __name__ == "__main__":
pimg = PasteImg()
pimg.main()