Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageGrab.py 4.6KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #
  2. # The Python Imaging Library
  3. # $Id$
  4. #
  5. # screen grabber
  6. #
  7. # History:
  8. # 2001-04-26 fl created
  9. # 2001-09-17 fl use builtin driver, if present
  10. # 2002-11-19 fl added grabclipboard support
  11. #
  12. # Copyright (c) 2001-2002 by Secret Labs AB
  13. # Copyright (c) 2001-2002 by Fredrik Lundh
  14. #
  15. # See the README file for information on usage and redistribution.
  16. #
  17. import os
  18. import shutil
  19. import subprocess
  20. import sys
  21. import tempfile
  22. from . import Image
  23. def grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=None):
  24. if xdisplay is None:
  25. if sys.platform == "darwin":
  26. fh, filepath = tempfile.mkstemp(".png")
  27. os.close(fh)
  28. args = ["screencapture"]
  29. if bbox:
  30. left, top, right, bottom = bbox
  31. args += ["-R", f"{left},{top},{right-left},{bottom-top}"]
  32. subprocess.call(args + ["-x", filepath])
  33. im = Image.open(filepath)
  34. im.load()
  35. os.unlink(filepath)
  36. if bbox:
  37. im_resized = im.resize((right - left, bottom - top))
  38. im.close()
  39. return im_resized
  40. return im
  41. elif sys.platform == "win32":
  42. offset, size, data = Image.core.grabscreen_win32(
  43. include_layered_windows, all_screens
  44. )
  45. im = Image.frombytes(
  46. "RGB",
  47. size,
  48. data,
  49. # RGB, 32-bit line padding, origin lower left corner
  50. "raw",
  51. "BGR",
  52. (size[0] * 3 + 3) & -4,
  53. -1,
  54. )
  55. if bbox:
  56. x0, y0 = offset
  57. left, top, right, bottom = bbox
  58. im = im.crop((left - x0, top - y0, right - x0, bottom - y0))
  59. return im
  60. elif shutil.which("gnome-screenshot"):
  61. fh, filepath = tempfile.mkstemp(".png")
  62. os.close(fh)
  63. subprocess.call(["gnome-screenshot", "-f", filepath])
  64. im = Image.open(filepath)
  65. im.load()
  66. os.unlink(filepath)
  67. if bbox:
  68. im_cropped = im.crop(bbox)
  69. im.close()
  70. return im_cropped
  71. return im
  72. # use xdisplay=None for default display on non-win32/macOS systems
  73. if not Image.core.HAVE_XCB:
  74. msg = "Pillow was built without XCB support"
  75. raise OSError(msg)
  76. size, data = Image.core.grabscreen_x11(xdisplay)
  77. im = Image.frombytes("RGB", size, data, "raw", "BGRX", size[0] * 4, 1)
  78. if bbox:
  79. im = im.crop(bbox)
  80. return im
  81. def grabclipboard():
  82. if sys.platform == "darwin":
  83. fh, filepath = tempfile.mkstemp(".jpg")
  84. os.close(fh)
  85. commands = [
  86. 'set theFile to (open for access POSIX file "'
  87. + filepath
  88. + '" with write permission)',
  89. "try",
  90. " write (the clipboard as JPEG picture) to theFile",
  91. "end try",
  92. "close access theFile",
  93. ]
  94. script = ["osascript"]
  95. for command in commands:
  96. script += ["-e", command]
  97. subprocess.call(script)
  98. im = None
  99. if os.stat(filepath).st_size != 0:
  100. im = Image.open(filepath)
  101. im.load()
  102. os.unlink(filepath)
  103. return im
  104. elif sys.platform == "win32":
  105. fmt, data = Image.core.grabclipboard_win32()
  106. if fmt == "file": # CF_HDROP
  107. import struct
  108. o = struct.unpack_from("I", data)[0]
  109. if data[16] != 0:
  110. files = data[o:].decode("utf-16le").split("\0")
  111. else:
  112. files = data[o:].decode("mbcs").split("\0")
  113. return files[: files.index("")]
  114. if isinstance(data, bytes):
  115. import io
  116. data = io.BytesIO(data)
  117. if fmt == "png":
  118. from . import PngImagePlugin
  119. return PngImagePlugin.PngImageFile(data)
  120. elif fmt == "DIB":
  121. from . import BmpImagePlugin
  122. return BmpImagePlugin.DibImageFile(data)
  123. return None
  124. else:
  125. if shutil.which("wl-paste"):
  126. args = ["wl-paste"]
  127. elif shutil.which("xclip"):
  128. args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"]
  129. else:
  130. msg = "wl-paste or xclip is required for ImageGrab.grabclipboard() on Linux"
  131. raise NotImplementedError(msg)
  132. fh, filepath = tempfile.mkstemp()
  133. subprocess.call(args, stdout=fh)
  134. os.close(fh)
  135. im = Image.open(filepath)
  136. im.load()
  137. os.unlink(filepath)
  138. return im