-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ImageGrab.grabclipboard() keeps unnecessary temporary files around #7147
Comments
with a bit of additional testing I think the suggested if err:
os.unlink(filepath)
msg = f"{args[0]} error: {err.strip().decode()}"
raise ChildProcessError(msg)
im = Image.open(filepath)
im.load()
os.unlink(filepath) should actually be something like if err:
os.unlink(filepath)
msg = f"{args[0]} error: {err.strip().decode()}"
raise ChildProcessError(msg)
im = None
try:
im = Image.open(filepath)
im.load()
except UnidentifiedImageError:
pass
os.unlink(filepath) as after the first paste
and simply write whatever text is in the clipboard to |
How so? |
In the sense that it should return |
I've created PR #7148 to remove the temporary file if a Regarding your second suggestion though, on macOS, it is returning |
Sure, I agree. I phrased the suggestion like "something like..." because I'm not sure of the proper way to do it. I just want to point out that even specifying Looking at the code again, I'm not sure why a temporary file is even necessary in the first place. p = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if p.stderr:
msg = f"{args[0]} error: {p.stderr.strip().decode()}"
raise ChildProcessError(msg)
data = p.stdout
if isinstance(data, bytes):
import io
from PIL import PngImagePlugin
data = io.BytesIO(data)
try:
return PngImagePlugin.PngImageFile(data)
except SyntaxError:
pass
return None There's still the problem of determining whether the file failed to load because it isn't PNG data at all, e.g. text, or whether the file failed to load because Pillow failed to load it. I'm not familiar with the Pillow API enough to know how to tell the difference, does |
A bit orthogonal to this discussion, the latest stable documentation (9.5.0) for It still says
when Linux support was added in 9.4.0 Something like
has not quite been substantiated yet. |
In your first comment, you say that xclip doesn't print text data, and then the other comment you say that it does. Could you clarify? If you have found a way to get xclip to output text data from |
PR #7160 has been merged to update the The stable documentation will be updated once the next version of Pillow is released. |
Sometimes it errors, sometimes it just prints text.
Sure,
This is completely deterministic for me. If step 4 is not performed, i.e., if 2 is ran over and over again, it always errors with the message. But once the clipboard is updated by copying something, then it won't error again until a new shell is started. |
I'm unable to replicate. xclip.mp4 |
I've created PR #7200 to no longer use a temporary file. |
Thanks!
It may be a quirk on my setup then, perhaps I should have specified that I was using the alacritty terminal emulator. One difference might be the terminal emulator, in the clip you selected the text xclip.mp4 |
If you've found a situation where xclip will just print out whatever it has, isn't it possible that the user copied a JPEG file, and that the data xclip is outputting on the second run is JPEG data that Pillow would currently understand? In case, returning I don't think there's any conceptual way to distinguish between "this isn't image data" and "this is image data that Pillow can't successfully load". |
Closing this issue as no feedback has been received. |
What did you do?
Call
PIL.ImageGrab.grabclipboard()
repeatedly.What did you expect to happen?
A constant number of temp files to be created.
What actually happened?
The number of files in
/tmp
keeps increasing.If there not an actual image in the clipboard, e.g. only text, then
will error with the message
while this error is caught on the latest Pillow (lines 146--148), the newly created tempfile is never removed.
Pillow/src/PIL/ImageGrab.py
Lines 146 to 151 in ab3d0c0
This can cause tempfiles to accumulate with repeated calls of
grabclipboard()
.I think
os.unlink(filepath)
should be called beforeChildProcessError
is raised to remove the now unnecessary file, i.e.What are your OS, Python and Pillow versions?
The text was updated successfully, but these errors were encountered: