blob: 7cc3b47cf0683074c2d478dcaea5903696c498d7 [file] [log] [blame]
Guido van Rossum2db91351992-10-18 17:09:59 +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.
8
9import os
10import tempfile
11import pipes
12import imghdr
13
14table = {}
15
Guido van Rossum7bc817d1993-12-17 15:25:27 +000016t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000017t.append('fromppm $IN $OUT', 'ff')
18table['ppm'] = t
19
Guido van Rossum7bc817d1993-12-17 15:25:27 +000020t = pipes.Template()
Sjoerd Mullender35fe6ec1993-10-11 12:39:15 +000021t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
Guido van Rossum2db91351992-10-18 17:09:59 +000022t.append('fromppm $IN $OUT', 'ff')
23table['pnm'] = t
24table['pgm'] = t
25table['pbm'] = t
26
Guido van Rossum7bc817d1993-12-17 15:25:27 +000027t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000028t.append('fromgif $IN $OUT', 'ff')
29table['gif'] = t
30
Guido van Rossum7bc817d1993-12-17 15:25:27 +000031t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000032t.append('tifftopnm', '--')
33t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
34t.append('fromppm $IN $OUT', 'ff')
35table['tiff'] = t
36
Guido van Rossum7bc817d1993-12-17 15:25:27 +000037t = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000038t.append('rasttopnm', '--')
Sjoerd Mullender35fe6ec1993-10-11 12:39:15 +000039t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
Guido van Rossum2db91351992-10-18 17:09:59 +000040t.append('fromppm $IN $OUT', 'ff')
41table['rast'] = t
42
Guido van Rossum7bc817d1993-12-17 15:25:27 +000043t = pipes.Template()
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000044t.append('djpeg', '--')
Sjoerd Mullender35fe6ec1993-10-11 12:39:15 +000045t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
Guido van Rossum5cfa5df1993-06-23 09:30:50 +000046t.append('fromppm $IN $OUT', 'ff')
47table['jpeg'] = t
48
Guido van Rossum7bc817d1993-12-17 15:25:27 +000049uncompress = pipes.Template()
Guido van Rossum2db91351992-10-18 17:09:59 +000050uncompress.append('uncompress', '--')
51
52
Fred Drakedef00382000-08-18 14:59:33 +000053class error(Exception):
54 pass
Guido van Rossum2db91351992-10-18 17:09:59 +000055
56def torgb(filename):
57 temps = []
58 ret = None
59 try:
60 ret = _torgb(filename, temps)
61 finally:
62 for temp in temps[:]:
Fred Drake132dce22000-12-12 23:11:42 +000063 if temp != ret:
Guido van Rossum2db91351992-10-18 17:09:59 +000064 try:
65 os.unlink(temp)
66 except os.error:
67 pass
68 temps.remove(temp)
69 return ret
70
71def _torgb(filename, temps):
72 if filename[-2:] == '.Z':
73 fname = tempfile.mktemp()
74 temps.append(fname)
75 sts = uncompress.copy(filename, fname)
76 if sts:
77 raise error, filename + ': uncompress failed'
78 else:
79 fname = filename
80 try:
81 ftype = imghdr.what(fname)
82 except IOError, msg:
83 if type(msg) == type(()) and len(msg) == 2 and \
84 type(msg[0]) == type(0) and type(msg[1]) == type(''):
85 msg = msg[1]
Fred Drake132dce22000-12-12 23:11:42 +000086 if type(msg) is not type(''):
Guido van Rossum2db91351992-10-18 17:09:59 +000087 msg = `msg`
88 raise error, filename + ': ' + msg
89 if ftype == 'rgb':
90 return fname
Fred Drake132dce22000-12-12 23:11:42 +000091 if ftype is None or not table.has_key(ftype):
Guido van Rossum2db91351992-10-18 17:09:59 +000092 raise error, \
93 filename + ': unsupported image file type ' + `ftype`
94 temp = tempfile.mktemp()
95 sts = table[ftype].copy(fname, temp)
96 if sts:
97 raise error, filename + ': conversion to rgb failed'
98 return temp