| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from warnings import warnpy3k |
| 9 |
warnpy3k("the torgb module has been removed in Python 3.0", stacklevel=2) |
| 10 |
del warnpy3k |
| 11 |
|
| 12 |
import os |
| 13 |
import tempfile |
| 14 |
import pipes |
| 15 |
import imghdr |
| 16 |
|
| 17 |
table = {} |
| 18 |
|
| 19 |
t = pipes.Template() |
| 20 |
t.append('fromppm $IN $OUT', 'ff') |
| 21 |
table['ppm'] = t |
| 22 |
|
| 23 |
t = pipes.Template() |
| 24 |
t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--') |
| 25 |
t.append('fromppm $IN $OUT', 'ff') |
| 26 |
table['pnm'] = t |
| 27 |
table['pgm'] = t |
| 28 |
table['pbm'] = t |
| 29 |
|
| 30 |
t = pipes.Template() |
| 31 |
t.append('fromgif $IN $OUT', 'ff') |
| 32 |
table['gif'] = t |
| 33 |
|
| 34 |
t = pipes.Template() |
| 35 |
t.append('tifftopnm', '--') |
| 36 |
t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--') |
| 37 |
t.append('fromppm $IN $OUT', 'ff') |
| 38 |
table['tiff'] = t |
| 39 |
|
| 40 |
t = pipes.Template() |
| 41 |
t.append('rasttopnm', '--') |
| 42 |
t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--') |
| 43 |
t.append('fromppm $IN $OUT', 'ff') |
| 44 |
table['rast'] = t |
| 45 |
|
| 46 |
t = pipes.Template() |
| 47 |
t.append('djpeg', '--') |
| 48 |
t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--') |
| 49 |
t.append('fromppm $IN $OUT', 'ff') |
| 50 |
table['jpeg'] = t |
| 51 |
|
| 52 |
uncompress = pipes.Template() |
| 53 |
uncompress.append('uncompress', '--') |
| 54 |
|
| 55 |
|
| 56 |
class error(Exception): |
| 57 |
pass |
| 58 |
|
| 59 |
def 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 |
|
| 74 |
def _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 |