• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Convert "arbitrary" image files to rgb files (SGI's image format).
2# Input may be compressed.
3# The uncompressed file type may be PBM, PGM, PPM, GIF, TIFF, or Sun raster.
4# An exception is raised if the file is not of a recognized type.
5# Returned filename is either the input filename or a temporary filename;
6# in the latter case the caller must ensure that it is removed.
7# Other temporary files used are removed by the function.
8from warnings import warnpy3k
9warnpy3k("the torgb module has been removed in Python 3.0", stacklevel=2)
10del warnpy3k
11
12import os
13import tempfile
14import pipes
15import imghdr
16
17table = {}
18
19t = pipes.Template()
20t.append('fromppm $IN $OUT', 'ff')
21table['ppm'] = t
22
23t = pipes.Template()
24t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
25t.append('fromppm $IN $OUT', 'ff')
26table['pnm'] = t
27table['pgm'] = t
28table['pbm'] = t
29
30t = pipes.Template()
31t.append('fromgif $IN $OUT', 'ff')
32table['gif'] = t
33
34t = pipes.Template()
35t.append('tifftopnm', '--')
36t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
37t.append('fromppm $IN $OUT', 'ff')
38table['tiff'] = t
39
40t = pipes.Template()
41t.append('rasttopnm', '--')
42t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
43t.append('fromppm $IN $OUT', 'ff')
44table['rast'] = t
45
46t = pipes.Template()
47t.append('djpeg', '--')
48t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
49t.append('fromppm $IN $OUT', 'ff')
50table['jpeg'] = t
51
52uncompress = pipes.Template()
53uncompress.append('uncompress', '--')
54
55
56class error(Exception):
57    pass
58
59def torgb(filename):
60    temps = []
61    ret = None
62    try:
63        ret = _torgb(filename, temps)
64    finally:
65        for temp in temps[:]:
66            if temp != ret:
67                try:
68                    os.unlink(temp)
69                except os.error:
70                    pass
71                temps.remove(temp)
72    return ret
73
74def _torgb(filename, temps):
75    if filename[-2:] == '.Z':
76        (fd, fname) = tempfile.mkstemp()
77        os.close(fd)
78        temps.append(fname)
79        sts = uncompress.copy(filename, fname)
80        if sts:
81            raise error, filename + ': uncompress failed'
82    else:
83        fname = filename
84    try:
85        ftype = imghdr.what(fname)
86    except IOError, msg:
87        if type(msg) == type(()) and len(msg) == 2 and \
88                type(msg[0]) == type(0) and type(msg[1]) == type(''):
89            msg = msg[1]
90        if type(msg) is not type(''):
91            msg = repr(msg)
92        raise error, filename + ': ' + msg
93    if ftype == 'rgb':
94        return fname
95    if ftype is None or not table.has_key(ftype):
96        raise error, '%s: unsupported image file type %r' % (filename, ftype)
97    (fd, temp) = tempfile.mkstemp()
98    os.close(fd)
99    sts = table[ftype].copy(fname, temp)
100    if sts:
101        raise error, filename + ': conversion to rgb failed'
102    return temp
103