home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Lib / sgi / torgb.py < prev   
Text File  |  1993-12-17  |  2KB  |  98 lines

  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.  
  16. t = pipes.Template()
  17. t.append('fromppm $IN $OUT', 'ff')
  18. table['ppm'] = t
  19.  
  20. t = pipes.Template()
  21. t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
  22. t.append('fromppm $IN $OUT', 'ff')
  23. table['pnm'] = t
  24. table['pgm'] = t
  25. table['pbm'] = t
  26.  
  27. t = pipes.Template()
  28. t.append('fromgif $IN $OUT', 'ff')
  29. table['gif'] = t
  30.  
  31. t = pipes.Template()
  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.  
  37. t = pipes.Template()
  38. t.append('rasttopnm', '--')
  39. t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
  40. t.append('fromppm $IN $OUT', 'ff')
  41. table['rast'] = t
  42.  
  43. t = pipes.Template()
  44. t.append('djpeg', '--')
  45. t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
  46. t.append('fromppm $IN $OUT', 'ff')
  47. table['jpeg'] = t
  48.  
  49. uncompress = pipes.Template()
  50. uncompress.append('uncompress', '--')
  51.  
  52.  
  53. error = 'torgb.error' # Exception
  54.  
  55. def torgb(filename):
  56.     temps = []
  57.     ret = None
  58.     try:
  59.         ret = _torgb(filename, temps)
  60.     finally:
  61.         for temp in temps[:]:
  62.             if temp <> ret:
  63.                 try:
  64.                     os.unlink(temp)
  65.                 except os.error:
  66.                     pass
  67.                 temps.remove(temp)
  68.     return ret
  69.  
  70. def _torgb(filename, temps):
  71.     if filename[-2:] == '.Z':
  72.         fname = tempfile.mktemp()
  73.         temps.append(fname)
  74.         sts = uncompress.copy(filename, fname)
  75.         if sts:
  76.             raise error, filename + ': uncompress failed'
  77.     else:
  78.         fname = filename
  79.     try:
  80.         ftype = imghdr.what(fname)
  81.     except IOError, msg:
  82.         if type(msg) == type(()) and len(msg) == 2 and \
  83.             type(msg[0]) == type(0) and type(msg[1]) == type(''):
  84.             msg = msg[1]
  85.         if type(msg) <> type(''):
  86.             msg = `msg`
  87.         raise error, filename + ': ' + msg
  88.     if ftype == 'rgb':
  89.         return fname
  90.     if ftype == None or not table.has_key(ftype):
  91.         raise error, \
  92.             filename + ': unsupported image file type ' + `ftype`
  93.     temp = tempfile.mktemp()
  94.     sts = table[ftype].copy(fname, temp)
  95.     if sts:
  96.         raise error, filename + ': conversion to rgb failed'
  97.     return temp
  98.