/Lib/plat-irix5/torgb.py

http://unladen-swallow.googlecode.com/ · Python · 102 lines · 82 code · 13 blank · 7 comment · 20 complexity · 1f4b6b241108e304d4abb10cad21bdcc MD5 · raw file

  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. from warnings import warnpy3k
  9. warnpy3k("the torgb module has been removed in Python 3.0", stacklevel=2)
  10. del warnpy3k
  11. import os
  12. import tempfile
  13. import pipes
  14. import imghdr
  15. table = {}
  16. t = pipes.Template()
  17. t.append('fromppm $IN $OUT', 'ff')
  18. table['ppm'] = t
  19. t = pipes.Template()
  20. t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
  21. t.append('fromppm $IN $OUT', 'ff')
  22. table['pnm'] = t
  23. table['pgm'] = t
  24. table['pbm'] = t
  25. t = pipes.Template()
  26. t.append('fromgif $IN $OUT', 'ff')
  27. table['gif'] = t
  28. t = pipes.Template()
  29. t.append('tifftopnm', '--')
  30. t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
  31. t.append('fromppm $IN $OUT', 'ff')
  32. table['tiff'] = t
  33. t = pipes.Template()
  34. t.append('rasttopnm', '--')
  35. t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
  36. t.append('fromppm $IN $OUT', 'ff')
  37. table['rast'] = t
  38. t = pipes.Template()
  39. t.append('djpeg', '--')
  40. t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
  41. t.append('fromppm $IN $OUT', 'ff')
  42. table['jpeg'] = t
  43. uncompress = pipes.Template()
  44. uncompress.append('uncompress', '--')
  45. class error(Exception):
  46. pass
  47. def torgb(filename):
  48. temps = []
  49. ret = None
  50. try:
  51. ret = _torgb(filename, temps)
  52. finally:
  53. for temp in temps[:]:
  54. if temp != ret:
  55. try:
  56. os.unlink(temp)
  57. except os.error:
  58. pass
  59. temps.remove(temp)
  60. return ret
  61. def _torgb(filename, temps):
  62. if filename[-2:] == '.Z':
  63. (fd, fname) = tempfile.mkstemp()
  64. os.close(fd)
  65. temps.append(fname)
  66. sts = uncompress.copy(filename, fname)
  67. if sts:
  68. raise error, filename + ': uncompress failed'
  69. else:
  70. fname = filename
  71. try:
  72. ftype = imghdr.what(fname)
  73. except IOError, msg:
  74. if type(msg) == type(()) and len(msg) == 2 and \
  75. type(msg[0]) == type(0) and type(msg[1]) == type(''):
  76. msg = msg[1]
  77. if type(msg) is not type(''):
  78. msg = repr(msg)
  79. raise error, filename + ': ' + msg
  80. if ftype == 'rgb':
  81. return fname
  82. if ftype is None or not table.has_key(ftype):
  83. raise error, '%s: unsupported image file type %r' % (filename, ftype)
  84. (fd, temp) = tempfile.mkstemp()
  85. os.close(fd)
  86. sts = table[ftype].copy(fname, temp)
  87. if sts:
  88. raise error, filename + ': conversion to rgb failed'
  89. return temp