home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / img / Lib / imgsgi.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  3.4 KB  |  130 lines

  1. """
  2. Read and write SGI image files using the 'rgbimg' module
  3. """
  4. #
  5. # SGI image file reader/writer module
  6. #
  7. import rgbimg
  8. from imgformat import rgb, rgb_b2t, grey, grey_b2t, error
  9.  
  10. class reader:
  11.     """Object that reads an SGI image file. The 'format', 'width' and
  12.     'height' attributes describe the image"""
  13.     
  14.     def __init__(self, filename):
  15.         if type(filename) != type(''):
  16.             raise error, 'cannot handle SGI images from open files'
  17.     self._filename = filename
  18.     self.width, self.height = rgbimg.sizeofimage(filename)
  19.     self.format = rgb
  20.     self.format_choices = (rgb, rgb_b2t)
  21.  
  22.     def args(self):
  23.     return self.__dict__
  24.     
  25.     def read(self):
  26.     "read the actual image data"
  27.     
  28.     if self.format == rgb:
  29.         old = rgbimg.ttob(1)
  30.     elif self.format == rgb_b2t:
  31.         old = rgbimg.ttob(0)
  32.     else:
  33.         raise error, 'Unsupported image format: '+`self.format`
  34.     data = rgbimg.longimagedata(self._filename)
  35.     rgbimg.ttob(old)
  36.     return data
  37.  
  38.     def write(self, data):
  39.     "dummy method: you cannot write to a reader object"
  40.     
  41.     raise error, 'Cannot write() to reader'
  42.  
  43. class writer:
  44.     """Object that writes an SGI image file. The 'format', 'width' and
  45.     'height' attributes describe the image"""
  46.     
  47.     def __init__(self, filename):
  48.         if type(filename) != type(''):
  49.             raise error, 'cannot handle SGI images from open files'
  50.     self._filename = filename
  51.     self.format_choices = (rgb, rgb_b2t)
  52.     self.format = rgb
  53.     self.file_format = rgb
  54.     self.file_format_choices = (rgb, rgb_b2t, grey, grey_b2t)
  55.  
  56.     def args(self):
  57.     return self.__dict__
  58.     
  59.     def _get(self, attr):
  60.     try:
  61.         return getattr(self, attr)
  62.     except AttributeError:
  63.         raise error, "Required attribute '%s' missing"%attr
  64.  
  65.     def read(self):
  66.     "dummy method: you cannot read from a writer object"
  67.     
  68.     raise error, 'Cannot read() from writer'
  69.  
  70.  
  71.     def write(self, data):
  72.     "write data to the image file"
  73.     
  74.     w = self._get('width')
  75.     h = self._get('height')
  76.  
  77.     if not self.format in (rgb, rgb_b2t):
  78.         raise error, 'Unsupported image format: '+`self.format`
  79.     if not self.file_format in (rgb, rgb_b2t, grey, grey_b2t):
  80.         raise error, 'Unsupported file format: '+`self.format`
  81.  
  82.     upside_down = 0
  83.     if self.format in (rgb_b2t, grey_b2t):
  84.         upside_down = (not upside_down)
  85.     if self.file_format in (rgb_b2t, grey_b2t):
  86.         upside_down = (not upside_down)
  87.     if upside_down:
  88.         old = rgbimg.ttob(0)
  89.     else:
  90.         old = rgbimg.ttob(1)
  91.  
  92.     if w*h*4 != len(data):
  93.         raise error, 'Incorrect datasize'
  94.  
  95.     if self.file_format in (grey, grey_b2t):
  96.         depth = 1
  97.     else:
  98.         depth = 3
  99.         
  100.     data = rgbimg.longstoimage(data, w, h, depth, self._filename)
  101.     rgbimg.ttob(old)
  102.         
  103. def _test():
  104.     rdr = reader('test.rgb')
  105.     print 'Image size is',rdr.width,'by',rdr.height
  106.     data = rdr.read()
  107.     print 'Saving output'
  108.     wr = writer('test_out.rgb')
  109.     wr.width, wr.height = rdr.width, rdr.height
  110.     wr.file_format = rgb
  111.     wr.write(data)
  112.     print 'Saving upside-down output'
  113.     wr = writer('test_out_b2t.rgb')
  114.     wr.width, wr.height = rdr.width, rdr.height
  115.     wr.file_format = rgb_b2t
  116.     wr.write(data)
  117.     print 'Saving grey output'
  118.     wr = writer('test_out_grey.rgb')
  119.     wr.width, wr.height = rdr.width, rdr.height
  120.     wr.file_format = grey
  121.     wr.write(data)
  122.     print 'Saving grey upside-down output'
  123.     wr = writer('test_out_grey_b2t.rgb')
  124.     wr.width, wr.height = rdr.width, rdr.height
  125.     wr.file_format = grey_b2t
  126.     wr.write(data)
  127.     
  128. if __name__ == '__main__':
  129.     _test()
  130.