Skip to content

Commit 427221e

Browse files
committed
In show_file, use os.remove to remove temporary images
1 parent c930be0 commit 427221e

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

Diff for: src/PIL/ImageShow.py

+44-24
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import shutil
1616
import subprocess
1717
import sys
18-
import tempfile
1918
from shlex import quote
2019

2120
from PIL import Image
@@ -147,16 +146,15 @@ def get_command(self, file, **options):
147146

148147
def show_file(self, file, **options):
149148
"""Display given file"""
150-
fd, path = tempfile.mkstemp()
151-
with os.fdopen(fd, "w") as f:
152-
f.write(file)
153-
with open(path) as f:
154-
subprocess.Popen(
155-
["im=$(cat); open -a Preview.app $im; sleep 20; rm -f $im"],
156-
shell=True,
157-
stdin=f,
158-
)
159-
os.remove(path)
149+
subprocess.call(["open", "-a", "Preview.app", file])
150+
subprocess.Popen(
151+
[
152+
sys.executable,
153+
"-c",
154+
"import os, sys, time;time.sleep(20);os.remove(sys.argv[1])",
155+
file,
156+
]
157+
)
160158
return 1
161159

162160

@@ -172,19 +170,6 @@ def get_command(self, file, **options):
172170
command = self.get_command_ex(file, **options)[0]
173171
return f"({command} {quote(file)}; rm -f {quote(file)})&"
174172

175-
def show_file(self, file, **options):
176-
"""Display given file"""
177-
fd, path = tempfile.mkstemp()
178-
with os.fdopen(fd, "w") as f:
179-
f.write(file)
180-
with open(path) as f:
181-
command = self.get_command_ex(file, **options)[0]
182-
subprocess.Popen(
183-
["im=$(cat);" + command + " $im; rm -f $im"], shell=True, stdin=f
184-
)
185-
os.remove(path)
186-
return 1
187-
188173

189174
class XDGViewer(UnixViewer):
190175
"""
@@ -195,6 +180,11 @@ def get_command_ex(self, file, **options):
195180
command = executable = "xdg-open"
196181
return command, executable
197182

183+
def show_file(self, file, **options):
184+
subprocess.Popen(["xdg-open", file])
185+
os.remove(file)
186+
return 1
187+
198188

199189
class DisplayViewer(UnixViewer):
200190
"""
@@ -208,6 +198,16 @@ def get_command_ex(self, file, title=None, **options):
208198
command += f" -name {quote(title)}"
209199
return command, executable
210200

201+
def show_file(self, file, **options):
202+
args = ["display"]
203+
if "title" in options:
204+
args += ["-name", options["title"]]
205+
args.append(file)
206+
207+
subprocess.Popen(args)
208+
os.remove(file)
209+
return 1
210+
211211

212212
class GmDisplayViewer(UnixViewer):
213213
"""The GraphicsMagick ``gm display`` command."""
@@ -217,6 +217,11 @@ def get_command_ex(self, file, **options):
217217
command = "gm display"
218218
return command, executable
219219

220+
def show_file(self, file, **options):
221+
subprocess.Popen(["gm", "display", file])
222+
os.remove(file)
223+
return 1
224+
220225

221226
class EogViewer(UnixViewer):
222227
"""The GNOME Image Viewer ``eog`` command."""
@@ -226,6 +231,11 @@ def get_command_ex(self, file, **options):
226231
command = "eog -n"
227232
return command, executable
228233

234+
def show_file(self, file, **options):
235+
subprocess.Popen(["eog", "-n", file])
236+
os.remove(file)
237+
return 1
238+
229239

230240
class XVViewer(UnixViewer):
231241
"""
@@ -241,6 +251,16 @@ def get_command_ex(self, file, title=None, **options):
241251
command += f" -name {quote(title)}"
242252
return command, executable
243253

254+
def show_file(self, file, **options):
255+
args = ["xv"]
256+
if "title" in options:
257+
args += ["-name", options["title"]]
258+
args.append(file)
259+
260+
subprocess.Popen(args)
261+
os.remove(file)
262+
return 1
263+
244264

245265
if sys.platform not in ("win32", "darwin"): # unixoids
246266
if shutil.which("xdg-open"):

0 commit comments

Comments
 (0)