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
| class ScreenCap(object):
def __init__(self):
"""Screenshot class"""
if sys.platform == 'linux2':
self.grab = self._GarbLinux
elif sys.platform == 'win32':
import Image, ImageGrab
self.grab = self._GrabWin
elif sys.platform == 'darwin':
self.grab = self._GrabMac
else:
sys.exit(1)
self._currentf = None
def _FicNormalyze(self, name):
"""Internal Function for generate temp file.
Use home user directory for rights.
"""
if name:
if os.path.basename(name) == name:
_grabfile = os.path.join(os.path.expanduser('~'), name)
else:
_grabfile = name
else:
# default name
_grabfile = os.path.join(os.path.expanduser('~'), 'scrcap.jpg')
self._currentf = _grabfile
return _grabfile
def _GarbLinux(self, name=None):
"""Internal function for screen capture under Linux
Use ImageMagick import utility
"""
_grabfile = self._FicNormalyze(name)
_grabcommand = "import -silent -window root " + _grabfile
os.system(_grabcommand)
return _grabfile
def _GrabWin(self, name=None):
"""Internal function for screen capture under Windows.
Use PIL ImageGrab.
"""
try:
import ImageGrab
except:
# Experimental import for PIL under Python 3.
from PIL import ImageGrab
_grabfile = self._FicNormalyze(name)
ImageGrab.grab().save(_grabfile, "JPEG")
return _grabfile
def _GrabMac(self, name=None):
"""Internal function for screen capture under Mac.
Use screencapture command.
"""
_grabfile = self._FicNormalyze(name)
_grabcommand = "screencapture -m -x -t jpg " + _grabfile
os.system(_grabcommand)
return _grabfile
... |
Partager