ImageGrab.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. raise OSError("Pillow was built without XCB support")
  75. size, data = Image.core.grabscreen_x11(xdisplay)
  76. im = Image.frombytes("RGB", size, data, "raw", "BGRX", size[0] * 4, 1)
  77. if bbox:
  78. im = im.crop(bbox)
  79. return im
  80. def grabclipboard():
  81. if sys.platform == "darwin":
  82. fh, filepath = tempfile.mkstemp(".jpg")
  83. os.close(fh)
  84. commands = [
  85. 'set theFile to (open for access POSIX file "'
  86. + filepath
  87. + '" with write permission)',
  88. "try",
  89. " write (the clipboard as JPEG picture) to theFile",
  90. "end try",
  91. "close access theFile",
  92. ]
  93. script = ["osascript"]
  94. for command in commands:
  95. script += ["-e", command]
  96. subprocess.call(script)
  97. im = None
  98. if os.stat(filepath).st_size != 0:
  99. im = Image.open(filepath)
  100. im.load()
  101. os.unlink(filepath)
  102. return im
  103. elif sys.platform == "win32":
  104. fmt, data = Image.core.grabclipboard_win32()
  105. if fmt == "file": # CF_HDROP
  106. import struct
  107. o = struct.unpack_from("I", data)[0]
  108. if data[16] != 0:
  109. files = data[o:].decode("utf-16le").split("\0")
  110. else:
  111. files = data[o:].decode("mbcs").split("\0")
  112. return files[: files.index("")]
  113. if isinstance(data, bytes):
  114. import io
  115. data = io.BytesIO(data)
  116. if fmt == "png":
  117. from . import PngImagePlugin
  118. return PngImagePlugin.PngImageFile(data)
  119. elif fmt == "DIB":
  120. from . import BmpImagePlugin
  121. return BmpImagePlugin.DibImageFile(data)
  122. return None
  123. else:
  124. raise NotImplementedError("ImageGrab.grabclipboard() is macOS and Windows only")