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