Last active
May 5, 2016 20:19
-
-
Save techtonik/4009c397f8f8aff31a46 to your computer and use it in GitHub Desktop.
save clipboard image to file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# the code is in public domain | |
# gist:4009c397f8f8aff31a46 | |
# | |
# history: | |
# 1.2 - rename to saveclip.py | |
# 1.1 - mention what library should be installed | |
# 1.0 - make get_avail_filename() function | |
__version__ = '1.2' | |
import os, sys | |
try: | |
from PIL import ImageGrab | |
except ImportError: | |
# https://github.com/python-pillow/Pillow/issues/1293 | |
raise ImportError("Pillow is not installed (use Pillow>=3.3.0)") | |
def get_avail_filename(basename, suffix='%0.2d.png'): | |
"""Find first non-existing filename with given numerical suffix""" | |
counter = 1 | |
while True: | |
newname = basename + suffix % counter | |
if not os.path.exists(newname): | |
return newname | |
counter += 1 | |
filename = 'clipboard.png' | |
if os.path.exists(filename): | |
filename = get_avail_filename('clipboard', suffix='%0.2d.png') | |
im = ImageGrab.grabclipboard() | |
if im is None: | |
sys.exit('Error: No image data in clipboard') | |
else: | |
im.save(filename, 'PNG') | |
print('Saved as %s' % filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment