| 1 |
""" |
| 2 |
PIL-based formats to take screenshots and grab from the clipboard. |
| 3 |
""" |
| 4 |
|
| 5 |
import threading |
| 6 |
|
| 7 |
import numpy as np |
| 8 |
|
| 9 |
from ..core import Format |
| 10 |
|
| 11 |
|
| 12 |
class BaseGrabFormat(Format): |
| 13 |
"""Base format for grab formats.""" |
| 14 |
|
| 15 |
_pillow_imported = False |
| 16 |
_ImageGrab = None |
| 17 |
|
| 18 |
def __init__(self, *args, **kwargs): |
| 19 |
super(BaseGrabFormat, self).__init__(*args, **kwargs) |
| 20 |
self._lock = threading.RLock() |
| 21 |
|
| 22 |
def _can_write(self, request): |
| 23 |
return False |
| 24 |
|
| 25 |
def _init_pillow(self): |
| 26 |
with self._lock: |
| 27 |
if not self._pillow_imported: |
| 28 |
self._pillow_imported = True |
| 29 |
import PIL |
| 30 |
|
| 31 |
if not hasattr(PIL, "__version__"): |
| 32 |
raise ImportError("Imageio Pillow requires " "Pillow, not PIL!") |
| 33 |
try: |
| 34 |
from PIL import ImageGrab |
| 35 |
except ImportError: |
| 36 |
return None |
| 37 |
self._ImageGrab = ImageGrab |
| 38 |
return self._ImageGrab |
| 39 |
|
| 40 |
class Reader(Format.Reader): |
| 41 |
def _open(self): |
| 42 |
pass |
| 43 |
|
| 44 |
def _close(self): |
| 45 |
pass |
| 46 |
|
| 47 |
def _get_data(self, index): |
| 48 |
return self.format._get_data(index) |
| 49 |
|
| 50 |
|
| 51 |
class ScreenGrabFormat(BaseGrabFormat): |
| 52 |
"""The ScreenGrabFormat provided a means to grab screenshots using |
| 53 |
the uri of "<screen>". |
| 54 |
|
| 55 |
This functionality is provided via Pillow. Note that "<screen>" is |
| 56 |
only supported on Windows and OS X. |
| 57 |
|
| 58 |
Parameters for reading |
| 59 |
---------------------- |
| 60 |
No parameters. |
| 61 |
""" |
| 62 |
|
| 63 |
def _can_read(self, request): |
| 64 |
if request.filename != "<screen>": |
| 65 |
return False |
| 66 |
return bool(self._init_pillow()) |
| 67 |
|
| 68 |
def _get_data(self, index): |
| 69 |
ImageGrab = self._init_pillow() |
| 70 |
assert ImageGrab |
| 71 |
|
| 72 |
pil_im = ImageGrab.grab() |
| 73 |
assert pil_im is not None |
| 74 |
im = np.asarray(pil_im) |
| 75 |
return im, {} |
| 76 |
|
| 77 |
|
| 78 |
class ClipboardGrabFormat(BaseGrabFormat): |
| 79 |
"""The ClipboardGrabFormat provided a means to grab image data from |
| 80 |
the clipboard, using the uri "<clipboard>" |
| 81 |
|
| 82 |
This functionality is provided via Pillow. Note that "<clipboard>" is |
| 83 |
only supported on Windows. |
| 84 |
|
| 85 |
Parameters for reading |
| 86 |
---------------------- |
| 87 |
No parameters. |
| 88 |
""" |
| 89 |
|
| 90 |
def _can_read(self, request): |
| 91 |
if request.filename != "<clipboard>": |
| 92 |
return False |
| 93 |
return bool(self._init_pillow()) |
| 94 |
|
| 95 |
def _get_data(self, index): |
| 96 |
ImageGrab = self._init_pillow() |
| 97 |
assert ImageGrab |
| 98 |
|
| 99 |
pil_im = ImageGrab.grabclipboard() |
| 100 |
if pil_im is None: |
| 101 |
raise RuntimeError( |
| 102 |
"There seems to be no image data on the " "clipboard now." |
| 103 |
) |
| 104 |
im = np.asarray(pil_im) |
| 105 |
return im, {} |