6
# Objects to convert an image format to another. Actually use the underlying
14
def system(self, cmd, doexec=1):
25
def convert(self, input, output, format, doexec=1):
29
class GifConverter(ImageConverter):
30
def convert(self, input, output, format, doexec=1):
31
cmd = "convert \"%s\" %s" % (input, output)
32
return self.system(cmd, doexec)
34
class EpsConverter(ImageConverter):
35
def convert(self, input, output, format, doexec=1):
37
cmd = "epstopdf --outfile=%s \"%s\"" % (output, input)
39
cmd = "convert \"%s\" %s" % (input, output)
42
return self.system(cmd, doexec)
44
class FigConverter(ImageConverter):
45
def convert(self, input, output, format, doexec=1):
49
conv.debug = self.debug
50
epsfile = "tmp_fig.eps"
52
post += conv.convert(epsfile, output, format, doexec=0)
57
cmd = "fig2dev -L eps \"%s\" > %s" % (input, epsfile)
63
# The Imagedata class handles all the image transformation
64
# process, from the discovery of the actual image involved to
65
# the conversion process.
70
self.input_format = "png"
71
self.output_format = "pdf"
74
def convert(self, fig):
75
# First, scan the available formats
76
(realfig, ext) = self.scanformat(fig)
78
# No real file found, give up
80
print "Image '%s' not found" % fig
83
# Check if this image has been already converted
84
if self.converted.has_key(realfig):
85
print "Image '%s' already converted as %s" % \
86
(fig, self.converted[realfig])
87
return self.converted[realfig]
89
# No format found, take the default one
91
ext = self.input_format
93
# Natively supported format?
94
if (ext == self.output_format):
98
count = len(self.converted)
99
newfig = "fig%d.%s" % (count, self.output_format)
101
if (ext == "fig" and self.output_format in ("eps", "pdf", "png")):
102
conv = FigConverter()
104
conv = EpsConverter()
105
elif (ext in ("gif", "bmp")):
106
conv = GifConverter()
108
# Unknown conversion to do, or nothing to do
111
# Convert the image and put it in the cache
112
conv.convert(realfig, newfig, self.output_format)
113
self.converted[realfig] = newfig
116
def scanformat(self, fig):
117
(root, ext) = os.path.splitext(fig)
120
realfig = self.find(fig)
121
return (realfig, ext[1:])
123
# Lookup for the best suited available figure
124
if (self.output_format == "pdf"):
125
formats = ("png", "pdf", "jpg", "eps", "gif", "fig")
127
formats = ("eps", "fig", "pdf", "png")
129
for format in formats:
130
realfig = self.find("%s.%s" % (fig, format))
132
print "Found %s for '%s'" % (format, fig)
135
# Maybe a figure with no extension
137
realfig = self.find(fig)
140
return (realfig, format)
143
# First, the obvious absolute path case
144
if os.path.isabs(fig):
145
if os.path.isfile(fig):
150
# Then, look for the file in known paths
151
for path in self.paths:
152
realfig = os.path.join(path, fig)
153
if os.path.isfile(realfig):
158
def system(self, cmd):
161
# TODO: raise error when system call failed