2
# The Python Imaging Library.
8
# 2008-04-06 fl Created
10
# Copyright (c) Secret Labs AB 2008.
12
# See the README file for information on usage and redistribution.
20
def register(viewer, order=1):
22
if issubclass(viewer, Viewer):
25
pass # raised if viewer wasn't a class
27
_viewers.append(viewer)
29
_viewers.insert(0, viewer)
32
# Displays a given image.
34
# @param image An image object.
35
# @param title Optional title. Not all viewers can display the title.
36
# @param **options Additional viewer options.
37
# @return True if a suitable viewer was found, false otherwise.
39
def show(image, title=None, **options):
40
for viewer in _viewers:
41
if viewer.show(image, title=title, **options):
46
# Base class for viewers.
52
def show(self, image, **options):
54
# save temporary image to disk
55
if image.mode[:4] == "I;16":
57
# "I;16" isn't an 'official' mode, but we still want to
58
# provide a simple way to show 16-bit images.
60
# FIXME: auto-contrast if max() > 255?
62
base = Image.getmodebase(image.mode)
63
if base != image.mode and image.mode != "1":
64
image = image.convert(base)
66
self.show_image(image, **options)
72
def get_format(self, image):
73
# return format name, or None to save as PGM/PPM
76
def get_command(self, file, **options):
77
raise NotImplementedError
79
def save_image(self, image):
80
# save to temporary file, and return filename
81
return image._dump(format=self.get_format(image))
83
def show_image(self, image, **options):
85
return self.show_file(self.save_image(image), **options)
87
def show_file(self, file, **options):
89
os.system(self.get_command(file, **options))
92
# --------------------------------------------------------------------
94
if sys.platform == "win32":
96
class WindowsViewer(Viewer):
98
def get_command(self, file, **options):
99
return "start /wait %s && del /f %s" % (file, file)
101
register(WindowsViewer)
103
elif sys.platform == "darwin":
105
class MacViewer(Viewer):
107
def get_command(self, file, **options):
108
# on darwin open returns immediately resulting in the temp
109
# file removal while app is opening
110
command = "open -a /Applications/Preview.app"
111
command = "(%s %s; sleep 20; rm -f %s)&" % (command, file, file)
120
def which(executable):
121
path = os.environ.get("PATH")
124
for dirname in path.split(os.pathsep):
125
filename = os.path.join(dirname, executable)
126
if os.path.isfile(filename):
127
# FIXME: make sure it's executable
131
class UnixViewer(Viewer):
132
def show_file(self, file, **options):
133
command, executable = self.get_command_ex(file, **options)
134
command = "(%s %s; rm -f %s)&" % (command, file, file)
140
class DisplayViewer(UnixViewer):
141
def get_command_ex(self, file, **options):
142
command = executable = "display"
143
return command, executable
146
register(DisplayViewer)
148
class XVViewer(UnixViewer):
149
def get_command_ex(self, file, title=None, **options):
150
# note: xv is pretty outdated. most modern systems have
151
# imagemagick's display command instead.
152
command = executable = "xv"
154
# FIXME: do full escaping
155
command = command + " -name \"%s\"" % title
156
return command, executable
161
if __name__ == "__main__":
162
# usage: python ImageShow.py imagefile [title]
163
print show(Image.open(sys.argv[1]), *sys.argv[2:])