Skip to content

Commit

Permalink
In show_file, use os.remove to remove temporary images
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Feb 2, 2022
1 parent c930be0 commit 427221e
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions src/PIL/ImageShow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import shutil
import subprocess
import sys
import tempfile
from shlex import quote

from PIL import Image
Expand Down Expand Up @@ -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


Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -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."""
Expand All @@ -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."""
Expand All @@ -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):
"""
Expand All @@ -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"):
Expand Down

0 comments on commit 427221e

Please sign in to comment.