PyXR

c:\python24\lib \ test \ test_imgfile.py



0001 #! /usr/bin/env python
0002 
0003 """Simple test script for imgfile.c
0004    Roger E. Masse
0005 """
0006 
0007 from test.test_support import verbose, unlink, findfile
0008 
0009 import imgfile, uu, os
0010 
0011 
0012 def main():
0013 
0014     uu.decode(findfile('testrgb.uue'), 'test.rgb')
0015     uu.decode(findfile('greyrgb.uue'), 'greytest.rgb')
0016 
0017     # Test a 3 byte color image
0018     testimage('test.rgb')
0019 
0020     # Test a 1 byte greyscale image
0021     testimage('greytest.rgb')
0022 
0023     unlink('test.rgb')
0024     unlink('greytest.rgb')
0025 
0026 def testimage(name):
0027     """Run through the imgfile's battery of possible methods
0028        on the image passed in name.
0029     """
0030 
0031     import sys
0032     import os
0033 
0034     outputfile = '/tmp/deleteme'
0035 
0036     # try opening the name directly
0037     try:
0038         # This function returns a tuple (x, y, z) where x and y are the size
0039         # of the image in pixels and z is the number of bytes per pixel. Only
0040         # 3 byte RGB pixels and 1 byte greyscale pixels are supported.
0041         sizes = imgfile.getsizes(name)
0042     except imgfile.error:
0043         # get a more qualified path component of the script...
0044         if __name__ == '__main__':
0045             ourname = sys.argv[0]
0046         else: # ...or the full path of the module
0047             ourname = sys.modules[__name__].__file__
0048 
0049         parts = ourname.split(os.sep)
0050         parts[-1] = name
0051         name = os.sep.join(parts)
0052         sizes = imgfile.getsizes(name)
0053     if verbose:
0054         print 'Opening test image: %s, sizes: %s' % (name, str(sizes))
0055     # This function reads and decodes the image on the specified file,
0056     # and returns it as a python string. The string has either 1 byte
0057     # greyscale pixels or 4 byte RGBA pixels. The bottom left pixel
0058     # is the first in the string. This format is suitable to pass
0059     # to gl.lrectwrite, for instance.
0060     image = imgfile.read(name)
0061 
0062     # This function writes the RGB or greyscale data in data to
0063     # image file file. x and y give the size of the image, z is
0064     # 1 for 1 byte greyscale images or 3 for RGB images (which
0065     # are stored as 4 byte values of which only the lower three
0066     # bytes are used). These are the formats returned by gl.lrectread.
0067     if verbose:
0068         print 'Writing output file'
0069     imgfile.write (outputfile, image, sizes[0], sizes[1], sizes[2])
0070 
0071 
0072     if verbose:
0073         print 'Opening scaled test image: %s, sizes: %s' % (name, str(sizes))
0074     # This function is identical to read but it returns an image that
0075     # is scaled to the given x and y sizes. If the filter and blur
0076     # parameters are omitted scaling is done by simply dropping
0077     # or duplicating pixels, so the result will be less than perfect,
0078     # especially for computer-generated images.  Alternatively,
0079     # you can specify a filter to use to smoothen the image after
0080     # scaling. The filter forms supported are 'impulse', 'box',
0081     # 'triangle', 'quadratic' and 'gaussian'. If a filter is
0082     # specified blur is an optional parameter specifying the
0083     # blurriness of the filter. It defaults to 1.0.  readscaled
0084     # makes no attempt to keep the aspect ratio correct, so that
0085     # is the users' responsibility.
0086     if verbose:
0087         print 'Filtering with "impulse"'
0088     simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'impulse', 2.0)
0089 
0090     # This function sets a global flag which defines whether the
0091     # scan lines of the image are read or written from bottom to
0092     # top (flag is zero, compatible with SGI GL) or from top to
0093     # bottom(flag is one, compatible with X). The default is zero.
0094     if verbose:
0095         print 'Switching to X compatibility'
0096     imgfile.ttob (1)
0097 
0098     if verbose:
0099         print 'Filtering with "triangle"'
0100     simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'triangle', 3.0)
0101     if verbose:
0102         print 'Switching back to SGI compatibility'
0103     imgfile.ttob (0)
0104 
0105     if verbose: print 'Filtering with "quadratic"'
0106     simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'quadratic')
0107     if verbose: print 'Filtering with "gaussian"'
0108     simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'gaussian', 1.0)
0109 
0110     if verbose:
0111         print 'Writing output file'
0112     imgfile.write (outputfile, simage, sizes[0]/2, sizes[1]/2, sizes[2])
0113 
0114     os.unlink(outputfile)
0115 
0116 main()
0117 

Generated by PyXR 0.9.4
SourceForge.net Logo