diff --git a/src/PIL/ImageShow.py b/src/PIL/ImageShow.py index 2135293e5eb..6217eec8662 100644 --- a/src/PIL/ImageShow.py +++ b/src/PIL/ImageShow.py @@ -15,7 +15,6 @@ import shutil import subprocess import sys -import tempfile from shlex import quote from PIL import Image @@ -147,16 +146,15 @@ def get_command(self, file, **options): def show_file(self, file, **options): """Display given file""" - fd, path = tempfile.mkstemp() - with os.fdopen(fd, "w") as f: - f.write(file) - with open(path) as f: - subprocess.Popen( - ["im=$(cat); open -a Preview.app $im; sleep 20; rm -f $im"], - shell=True, - stdin=f, - ) - os.remove(path) + subprocess.call(["open", "-a", "Preview.app", file]) + subprocess.Popen( + [ + sys.executable, + "-c", + "import os, sys, time;time.sleep(20);os.remove(sys.argv[1])", + file, + ] + ) return 1 @@ -172,19 +170,6 @@ def get_command(self, file, **options): command = self.get_command_ex(file, **options)[0] return f"({command} {quote(file)}; rm -f {quote(file)})&" - def show_file(self, file, **options): - """Display given file""" - fd, path = tempfile.mkstemp() - with os.fdopen(fd, "w") as f: - f.write(file) - with open(path) as f: - command = self.get_command_ex(file, **options)[0] - subprocess.Popen( - ["im=$(cat);" + command + " $im; rm -f $im"], shell=True, stdin=f - ) - os.remove(path) - return 1 - class XDGViewer(UnixViewer): """ @@ -195,6 +180,11 @@ def get_command_ex(self, file, **options): command = executable = "xdg-open" return command, executable + def show_file(self, file, **options): + subprocess.Popen(["xdg-open", file]) + os.remove(file) + return 1 + class DisplayViewer(UnixViewer): """ @@ -208,6 +198,16 @@ def get_command_ex(self, file, title=None, **options): command += f" -name {quote(title)}" return command, executable + def show_file(self, file, **options): + args = ["display"] + if "title" in options: + args += ["-name", options["title"]] + args.append(file) + + subprocess.Popen(args) + os.remove(file) + return 1 + class GmDisplayViewer(UnixViewer): """The GraphicsMagick ``gm display`` command.""" @@ -217,6 +217,11 @@ def get_command_ex(self, file, **options): command = "gm display" return command, executable + def show_file(self, file, **options): + subprocess.Popen(["gm", "display", file]) + os.remove(file) + return 1 + class EogViewer(UnixViewer): """The GNOME Image Viewer ``eog`` command.""" @@ -226,6 +231,11 @@ def get_command_ex(self, file, **options): command = "eog -n" return command, executable + def show_file(self, file, **options): + subprocess.Popen(["eog", "-n", file]) + os.remove(file) + return 1 + class XVViewer(UnixViewer): """ @@ -241,6 +251,16 @@ def get_command_ex(self, file, title=None, **options): command += f" -name {quote(title)}" return command, executable + def show_file(self, file, **options): + args = ["xv"] + if "title" in options: + args += ["-name", options["title"]] + args.append(file) + + subprocess.Popen(args) + os.remove(file) + return 1 + if sys.platform not in ("win32", "darwin"): # unixoids if shutil.which("xdg-open"):