mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-02-12 09:30:48 +03:00
Merge pull request #8630 from radarhere/imagegrab_macos
Do not use temporary file in ImageGrab.grabclipboard() on macOS
This commit is contained in:
commit
4e82f2bec0
|
@ -104,28 +104,17 @@ def grab(
|
||||||
|
|
||||||
def grabclipboard() -> Image.Image | list[str] | None:
|
def grabclipboard() -> Image.Image | list[str] | None:
|
||||||
if sys.platform == "darwin":
|
if sys.platform == "darwin":
|
||||||
fh, filepath = tempfile.mkstemp(".png")
|
p = subprocess.run(
|
||||||
os.close(fh)
|
["osascript", "-e", "get the clipboard as «class PNGf»"],
|
||||||
commands = [
|
capture_output=True,
|
||||||
'set theFile to (open for access POSIX file "'
|
)
|
||||||
+ filepath
|
if p.returncode != 0:
|
||||||
+ '" with write permission)',
|
return None
|
||||||
"try",
|
|
||||||
" write (the clipboard as «class PNGf») to theFile",
|
|
||||||
"end try",
|
|
||||||
"close access theFile",
|
|
||||||
]
|
|
||||||
script = ["osascript"]
|
|
||||||
for command in commands:
|
|
||||||
script += ["-e", command]
|
|
||||||
subprocess.call(script)
|
|
||||||
|
|
||||||
im = None
|
import binascii
|
||||||
if os.stat(filepath).st_size != 0:
|
|
||||||
im = Image.open(filepath)
|
data = io.BytesIO(binascii.unhexlify(p.stdout[11:-3]))
|
||||||
im.load()
|
return Image.open(data)
|
||||||
os.unlink(filepath)
|
|
||||||
return im
|
|
||||||
elif sys.platform == "win32":
|
elif sys.platform == "win32":
|
||||||
fmt, data = Image.core.grabclipboard_win32()
|
fmt, data = Image.core.grabclipboard_win32()
|
||||||
if fmt == "file": # CF_HDROP
|
if fmt == "file": # CF_HDROP
|
||||||
|
|
Loading…
Reference in New Issue
Block a user