From a0be7b09cc89511435f7b4c0272b6a1c2a6b6a2a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 3 Nov 2018 13:13:04 +1100 Subject: [PATCH 1/2] Added UnixViewer get_command --- Tests/test_imageshow.py | 10 ++++++++++ src/PIL/ImageShow.py | 9 +++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Tests/test_imageshow.py b/Tests/test_imageshow.py index da91e35c77f..244c18d91e1 100644 --- a/Tests/test_imageshow.py +++ b/Tests/test_imageshow.py @@ -14,6 +14,9 @@ def test_register(self): # Test registering a viewer that is not a class ImageShow.register("not a class") + # Restore original state + ImageShow._viewers.pop() + def test_show(self): class TestViewer: methodCalled = False @@ -28,6 +31,9 @@ def show(self, image, title=None, **options): self.assertTrue(ImageShow.show(im)) self.assertTrue(viewer.methodCalled) + # Restore original state + ImageShow._viewers.pop(0) + def test_viewer(self): viewer = ImageShow.Viewer() @@ -35,6 +41,10 @@ def test_viewer(self): self.assertRaises(NotImplementedError, viewer.get_command, None) + def test_viewers(self): + for viewer in ImageShow._viewers: + viewer.get_command('test.jpg') + if __name__ == '__main__': unittest.main() diff --git a/src/PIL/ImageShow.py b/src/PIL/ImageShow.py index b50d6135884..f1819cc5c8e 100644 --- a/src/PIL/ImageShow.py +++ b/src/PIL/ImageShow.py @@ -148,12 +148,9 @@ class UnixViewer(Viewer): format = "PNG" options = {'compress_level': 1} - def show_file(self, file, **options): - command, executable = self.get_command_ex(file, **options) - command = "(%s %s; rm -f %s)&" % (command, quote(file), - quote(file)) - os.system(command) - return 1 + def get_command(self, file, **options): + command = self.get_command_ex(file, **options)[0] + return "(%s %s; rm -f %s)&" % (command, quote(file), quote(file)) # implementations From fea3dafd05c8ca44c30167c34ce4d82964421efb Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 3 Nov 2018 14:24:10 +1100 Subject: [PATCH 2/2] Supply filename through stdin instead of inline in Mac and Unix ImageShow viewers --- src/PIL/ImageShow.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/PIL/ImageShow.py b/src/PIL/ImageShow.py index f1819cc5c8e..7d758c737c8 100644 --- a/src/PIL/ImageShow.py +++ b/src/PIL/ImageShow.py @@ -17,6 +17,8 @@ from PIL import Image import os import sys +import subprocess +import tempfile if sys.version_info.major >= 3: from shlex import quote @@ -128,6 +130,21 @@ def get_command(self, file, **options): quote(file)) return command + def show_file(self, file, **options): + """Display given file""" + f, path = tempfile.mkstemp() + f.write(file) + f.close() + with open(path, "r") as f: + subprocess.Popen([ + 'im=$(cat);' + 'open -a /Applications/Preview.app $im;' + 'sleep 20;' + 'rm -f $im' + ], shell=True, stdin=f) + os.remove(path) + return 1 + register(MacViewer) else: @@ -152,6 +169,21 @@ def get_command(self, file, **options): command = self.get_command_ex(file, **options)[0] return "(%s %s; rm -f %s)&" % (command, quote(file), quote(file)) + def show_file(self, file, **options): + """Display given file""" + f, path = tempfile.mkstemp() + f.write(file) + f.close() + with open(path, "r") 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 + # implementations class DisplayViewer(UnixViewer):