Skip to content

Commit

Permalink
Merge pull request #7200 from radarhere/imagegrab
Browse files Browse the repository at this point in the history
Do not use temporary file when grabbing clipboard on Linux
  • Loading branch information
hugovk committed Jun 6, 2023
2 parents 3568df8 + 97bd533 commit 07be6aa
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/PIL/ImageGrab.py
Expand Up @@ -15,6 +15,7 @@
# See the README file for information on usage and redistribution.
#

import io
import os
import shutil
import subprocess
Expand Down Expand Up @@ -128,8 +129,6 @@ def grabclipboard():
files = data[o:].decode("mbcs").split("\0")
return files[: files.index("")]
if isinstance(data, bytes):
import io

data = io.BytesIO(data)
if fmt == "png":
from . import PngImagePlugin
Expand Down Expand Up @@ -159,13 +158,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)
data = io.BytesIO(p.stdout)
im = Image.open(data)
im.load()
os.unlink(filepath)
return im

0 comments on commit 07be6aa

Please sign in to comment.