blob: 024d1ea4889a45d9b88f896d3ca0d3596e14e623 [file] [log] [blame]
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +00001# 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.
Brett Cannonf56b6ae2008-05-15 04:15:25 +00008from warnings import warnpy3k
9warnpy3k("the torgb module has been removed in Python 3.0", stacklevel=2)
10del warnpy3k
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000011
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
Fred Drakedef00382000-08-18 14:59:33 +000056class error(Exception):
Tim Peters182b5ac2004-07-18 06:16:08 +000057 pass
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000058
59def torgb(filename):
Tim Peters182b5ac2004-07-18 06:16:08 +000060 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
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000073
74def _torgb(filename, temps):
Tim Peters182b5ac2004-07-18 06:16:08 +000075 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