mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-23 13:44:47 +03:00
Enable ImageGrab for linux
This commit is contained in:
parent
291f1eb1e2
commit
ab6e13ec83
|
@ -15,16 +15,13 @@
|
||||||
# See the README file for information on usage and redistribution.
|
# See the README file for information on usage and redistribution.
|
||||||
#
|
#
|
||||||
|
|
||||||
import os
|
|
||||||
import subprocess
|
|
||||||
import sys
|
import sys
|
||||||
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import subprocess
|
||||||
|
|
||||||
from . import Image
|
from . import Image
|
||||||
|
|
||||||
if sys.platform not in ["win32", "darwin"]:
|
|
||||||
raise ImportError("ImageGrab is macOS and Windows only")
|
|
||||||
|
|
||||||
|
|
||||||
def grab(bbox=None, include_layered_windows=False, all_screens=False):
|
def grab(bbox=None, include_layered_windows=False, all_screens=False):
|
||||||
if sys.platform == "darwin":
|
if sys.platform == "darwin":
|
||||||
|
@ -38,8 +35,9 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False):
|
||||||
im_cropped = im.crop(bbox)
|
im_cropped = im.crop(bbox)
|
||||||
im.close()
|
im.close()
|
||||||
return im_cropped
|
return im_cropped
|
||||||
else:
|
elif sys.platform == "win32":
|
||||||
offset, size, data = Image.core.grabscreen(include_layered_windows, all_screens)
|
grabber = Image.core.grabscreen
|
||||||
|
offset, size, data = grabber(include_layered_windows, all_screens)
|
||||||
im = Image.frombytes(
|
im = Image.frombytes(
|
||||||
"RGB",
|
"RGB",
|
||||||
size,
|
size,
|
||||||
|
@ -54,6 +52,15 @@ def grab(bbox=None, include_layered_windows=False, all_screens=False):
|
||||||
x0, y0 = offset
|
x0, y0 = offset
|
||||||
left, top, right, bottom = bbox
|
left, top, right, bottom = bbox
|
||||||
im = im.crop((left - x0, top - y0, right - x0, bottom - y0))
|
im = im.crop((left - x0, top - y0, right - x0, bottom - y0))
|
||||||
|
else:
|
||||||
|
fh, filepath = tempfile.mkstemp(".png")
|
||||||
|
os.close(fh)
|
||||||
|
subprocess.call(["import", "-window", "root", filepath])
|
||||||
|
im = Image.open(filepath)
|
||||||
|
im.load()
|
||||||
|
os.unlink(filepath)
|
||||||
|
if bbox:
|
||||||
|
im = im.crop(bbox)
|
||||||
return im
|
return im
|
||||||
|
|
||||||
|
|
||||||
|
@ -81,7 +88,8 @@ def grabclipboard():
|
||||||
im.load()
|
im.load()
|
||||||
os.unlink(filepath)
|
os.unlink(filepath)
|
||||||
return im
|
return im
|
||||||
else:
|
|
||||||
|
elif sys.platform == "win32":
|
||||||
data = Image.core.grabclipboard()
|
data = Image.core.grabclipboard()
|
||||||
if isinstance(data, bytes):
|
if isinstance(data, bytes):
|
||||||
from . import BmpImagePlugin
|
from . import BmpImagePlugin
|
||||||
|
@ -89,3 +97,4 @@ def grabclipboard():
|
||||||
|
|
||||||
return BmpImagePlugin.DibImageFile(io.BytesIO(data))
|
return BmpImagePlugin.DibImageFile(io.BytesIO(data))
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user