Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageShow improvements #3450

Merged
merged 2 commits into from Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions Tests/test_imageshow.py
Expand Up @@ -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
Expand All @@ -28,13 +31,20 @@ 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()

self.assertIsNone(viewer.get_format(None))

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()
37 changes: 33 additions & 4 deletions src/PIL/ImageShow.py
Expand Up @@ -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
Expand Down Expand Up @@ -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()
hugovk marked this conversation as resolved.
Show resolved Hide resolved
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:
Expand All @@ -148,11 +165,23 @@ class UnixViewer(Viewer):
format = "PNG"
options = {'compress_level': 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))

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)
"""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
Expand Down