Skip to content

Commit

Permalink
Merge pull request #6045 from radarhere/imageshow
Browse files Browse the repository at this point in the history
Do not automatically remove temporary ImageShow files on Unix
  • Loading branch information
hugovk committed Feb 19, 2022
2 parents 3d33987 + 60e3734 commit bfa6da6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
10 changes: 10 additions & 0 deletions Tests/test_imageshow.py
Expand Up @@ -51,6 +51,16 @@ def test_show():
assert ImageShow.show(im)


def test_show_without_viewers():
viewers = ImageShow._viewers
ImageShow._viewers = []

im = hopper()
assert not ImageShow.show(im)

ImageShow._viewers = viewers


def test_viewer():
viewer = ImageShow.Viewer()

Expand Down
7 changes: 7 additions & 0 deletions docs/releasenotes/9.1.0.rst
Expand Up @@ -114,6 +114,13 @@ breaking backwards compatibility.
Other Changes
=============

ImageShow temporary files on Unix
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

When calling :py:meth:`~PIL.Image.Image.show` or using :py:mod:`~PIL.ImageShow`,
a temporary file is created from the image. On Unix, Pillow will no longer delete these
files, and instead leave it to the operating system to do so.

Image._repr_pretty_
^^^^^^^^^^^^^^^^^^^

Expand Down
34 changes: 13 additions & 21 deletions src/PIL/ImageShow.py
Expand Up @@ -54,8 +54,8 @@ def show(image, title=None, **options):
"""
for viewer in _viewers:
if viewer.show(image, title=title, **options):
return 1
return 0
return True
return False


class Viewer:
Expand Down Expand Up @@ -126,16 +126,6 @@ def show_file(self, path=None, **options):
os.system(self.get_command(path, **options))
return 1

def _remove_path_after_delay(self, path):
subprocess.Popen(
[
sys.executable,
"-c",
"import os, sys, time; time.sleep(20); os.remove(sys.argv[1])",
path,
]
)


# --------------------------------------------------------------------

Expand Down Expand Up @@ -190,7 +180,14 @@ def show_file(self, path=None, **options):
else:
raise TypeError("Missing required argument: 'path'")
subprocess.call(["open", "-a", "Preview.app", path])
self._remove_path_after_delay(path)
subprocess.Popen(
[
sys.executable,
"-c",
"import os, sys, time; time.sleep(20); os.remove(sys.argv[1])",
path,
]
)
return 1


Expand All @@ -204,7 +201,7 @@ class UnixViewer(Viewer):

def get_command(self, file, **options):
command = self.get_command_ex(file, **options)[0]
return f"({command} {quote(file)}; rm -f {quote(file)})&"
return f"({command} {quote(file)}"


class XDGViewer(UnixViewer):
Expand Down Expand Up @@ -235,7 +232,6 @@ def show_file(self, path=None, **options):
else:
raise TypeError("Missing required argument: 'path'")
subprocess.Popen(["xdg-open", path])
self._remove_path_after_delay(path)
return 1


Expand All @@ -248,7 +244,7 @@ class DisplayViewer(UnixViewer):
def get_command_ex(self, file, title=None, **options):
command = executable = "display"
if title:
command += f" -name {quote(title)}"
command += f" -title {quote(title)}"
return command, executable

def show_file(self, path=None, **options):
Expand All @@ -270,11 +266,10 @@ def show_file(self, path=None, **options):
raise TypeError("Missing required argument: 'path'")
args = ["display"]
if "title" in options:
args += ["-name", options["title"]]
args += ["-title", options["title"]]
args.append(path)

subprocess.Popen(args)
os.remove(path)
return 1


Expand Down Expand Up @@ -304,7 +299,6 @@ def show_file(self, path=None, **options):
else:
raise TypeError("Missing required argument: 'path'")
subprocess.Popen(["gm", "display", path])
os.remove(path)
return 1


Expand Down Expand Up @@ -334,7 +328,6 @@ def show_file(self, path=None, **options):
else:
raise TypeError("Missing required argument: 'path'")
subprocess.Popen(["eog", "-n", path])
os.remove(path)
return 1


Expand Down Expand Up @@ -375,7 +368,6 @@ def show_file(self, path=None, **options):
args.append(path)

subprocess.Popen(args)
os.remove(path)
return 1


Expand Down

0 comments on commit bfa6da6

Please sign in to comment.