Skip to content

Commit

Permalink
Use pipe instead of temporary file for Linux grabclipboard()
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed May 13, 2023
1 parent e063ed7 commit cc2fb84
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/PIL/ImageGrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import subprocess
import sys
import tempfile
from io import BytesIO

from . import Image

Expand Down Expand Up @@ -140,13 +141,12 @@ def grabclipboard():
else:
msg = "wl-paste or xclip is required for ImageGrab.grabclipboard() on Linux"
raise NotImplementedError(msg)
fh, filepath = tempfile.mkstemp()
err = subprocess.run(args, stdout=fh, stderr=subprocess.PIPE).stderr
os.close(fh)
p = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
err = p.stderr
if err:
msg = f"{args[0]} error: {err.strip().decode()}"
raise ChildProcessError(msg)
im = Image.open(filepath)
im.load()
os.unlink(filepath)
with BytesIO(p.stdout) as fp:
im = Image.open(fp)
im.load()
return im

0 comments on commit cc2fb84

Please sign in to comment.