Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 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. |
| 8 | |
| 9 | import os |
| 10 | import tempfile |
| 11 | import pipes |
| 12 | import imghdr |
| 13 | |
| 14 | table = {} |
| 15 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 16 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 17 | t.append('fromppm $IN $OUT', 'ff') |
| 18 | table['ppm'] = t |
| 19 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 20 | t = pipes.Template() |
Sjoerd Mullender | 35fe6ec | 1993-10-11 12:39:15 +0000 | [diff] [blame] | 21 | t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--') |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 22 | t.append('fromppm $IN $OUT', 'ff') |
| 23 | table['pnm'] = t |
| 24 | table['pgm'] = t |
| 25 | table['pbm'] = t |
| 26 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 27 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 28 | t.append('fromgif $IN $OUT', 'ff') |
| 29 | table['gif'] = t |
| 30 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 31 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 32 | t.append('tifftopnm', '--') |
| 33 | t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--') |
| 34 | t.append('fromppm $IN $OUT', 'ff') |
| 35 | table['tiff'] = t |
| 36 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 37 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 38 | t.append('rasttopnm', '--') |
Sjoerd Mullender | 35fe6ec | 1993-10-11 12:39:15 +0000 | [diff] [blame] | 39 | t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--') |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 40 | t.append('fromppm $IN $OUT', 'ff') |
| 41 | table['rast'] = t |
| 42 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 43 | t = pipes.Template() |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 44 | t.append('djpeg', '--') |
Sjoerd Mullender | 35fe6ec | 1993-10-11 12:39:15 +0000 | [diff] [blame] | 45 | t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--') |
Guido van Rossum | 5cfa5df | 1993-06-23 09:30:50 +0000 | [diff] [blame] | 46 | t.append('fromppm $IN $OUT', 'ff') |
| 47 | table['jpeg'] = t |
| 48 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 49 | uncompress = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 50 | uncompress.append('uncompress', '--') |
| 51 | |
| 52 | |
Fred Drake | def0038 | 2000-08-18 14:59:33 +0000 | [diff] [blame] | 53 | class error(Exception): |
| 54 | pass |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 55 | |
| 56 | def torgb(filename): |
| 57 | temps = [] |
| 58 | ret = None |
| 59 | try: |
| 60 | ret = _torgb(filename, temps) |
| 61 | finally: |
| 62 | for temp in temps[:]: |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 63 | if temp != ret: |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 64 | try: |
| 65 | os.unlink(temp) |
| 66 | except os.error: |
| 67 | pass |
| 68 | temps.remove(temp) |
| 69 | return ret |
| 70 | |
| 71 | def _torgb(filename, temps): |
| 72 | if filename[-2:] == '.Z': |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 73 | (fd, fname) = tempfile.mkstemp() |
| 74 | os.close(fd) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 75 | temps.append(fname) |
| 76 | sts = uncompress.copy(filename, fname) |
| 77 | if sts: |
| 78 | raise error, filename + ': uncompress failed' |
| 79 | else: |
| 80 | fname = filename |
| 81 | try: |
| 82 | ftype = imghdr.what(fname) |
| 83 | except IOError, msg: |
| 84 | if type(msg) == type(()) and len(msg) == 2 and \ |
| 85 | type(msg[0]) == type(0) and type(msg[1]) == type(''): |
| 86 | msg = msg[1] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 87 | if type(msg) is not type(''): |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 88 | msg = `msg` |
| 89 | raise error, filename + ': ' + msg |
| 90 | if ftype == 'rgb': |
| 91 | return fname |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 92 | if ftype is None or not table.has_key(ftype): |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 93 | raise error, \ |
| 94 | filename + ': unsupported image file type ' + `ftype` |
Guido van Rossum | dd8ddac | 2002-08-10 00:17:59 +0000 | [diff] [blame] | 95 | (fd, temp) = tempfile.mkstemp() |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 96 | os.close(fd) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 97 | sts = table[ftype].copy(fname, temp) |
| 98 | if sts: |
| 99 | raise error, filename + ': conversion to rgb failed' |
| 100 | return temp |