/Lib/test/test_imgfile.py

http://unladen-swallow.googlecode.com/ · Python · 119 lines · 87 code · 17 blank · 15 comment · 8 complexity · 1080112662d1d1087d1bb173b6a7f052 MD5 · raw file

  1. #! /usr/bin/env python
  2. """Simple test script for imgfile.c
  3. Roger E. Masse
  4. """
  5. from test.test_support import verbose, unlink, findfile, import_module
  6. imgfile = import_module('imgfile', deprecated=True)
  7. import uu
  8. def testimage(name):
  9. """Run through the imgfile's battery of possible methods
  10. on the image passed in name.
  11. """
  12. import sys
  13. import os
  14. outputfile = '/tmp/deleteme'
  15. # try opening the name directly
  16. try:
  17. # This function returns a tuple (x, y, z) where x and y are the size
  18. # of the image in pixels and z is the number of bytes per pixel. Only
  19. # 3 byte RGB pixels and 1 byte greyscale pixels are supported.
  20. sizes = imgfile.getsizes(name)
  21. except imgfile.error:
  22. # get a more qualified path component of the script...
  23. if __name__ == '__main__':
  24. ourname = sys.argv[0]
  25. else: # ...or the full path of the module
  26. ourname = sys.modules[__name__].__file__
  27. parts = ourname.split(os.sep)
  28. parts[-1] = name
  29. name = os.sep.join(parts)
  30. sizes = imgfile.getsizes(name)
  31. if verbose:
  32. print 'Opening test image: %s, sizes: %s' % (name, str(sizes))
  33. # This function reads and decodes the image on the specified file,
  34. # and returns it as a python string. The string has either 1 byte
  35. # greyscale pixels or 4 byte RGBA pixels. The bottom left pixel
  36. # is the first in the string. This format is suitable to pass
  37. # to gl.lrectwrite, for instance.
  38. image = imgfile.read(name)
  39. # This function writes the RGB or greyscale data in data to
  40. # image file file. x and y give the size of the image, z is
  41. # 1 for 1 byte greyscale images or 3 for RGB images (which
  42. # are stored as 4 byte values of which only the lower three
  43. # bytes are used). These are the formats returned by gl.lrectread.
  44. if verbose:
  45. print 'Writing output file'
  46. imgfile.write (outputfile, image, sizes[0], sizes[1], sizes[2])
  47. if verbose:
  48. print 'Opening scaled test image: %s, sizes: %s' % (name, str(sizes))
  49. # This function is identical to read but it returns an image that
  50. # is scaled to the given x and y sizes. If the filter and blur
  51. # parameters are omitted scaling is done by simply dropping
  52. # or duplicating pixels, so the result will be less than perfect,
  53. # especially for computer-generated images. Alternatively,
  54. # you can specify a filter to use to smoothen the image after
  55. # scaling. The filter forms supported are 'impulse', 'box',
  56. # 'triangle', 'quadratic' and 'gaussian'. If a filter is
  57. # specified blur is an optional parameter specifying the
  58. # blurriness of the filter. It defaults to 1.0. readscaled
  59. # makes no attempt to keep the aspect ratio correct, so that
  60. # is the users' responsibility.
  61. if verbose:
  62. print 'Filtering with "impulse"'
  63. simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'impulse', 2.0)
  64. # This function sets a global flag which defines whether the
  65. # scan lines of the image are read or written from bottom to
  66. # top (flag is zero, compatible with SGI GL) or from top to
  67. # bottom(flag is one, compatible with X). The default is zero.
  68. if verbose:
  69. print 'Switching to X compatibility'
  70. imgfile.ttob (1)
  71. if verbose:
  72. print 'Filtering with "triangle"'
  73. simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'triangle', 3.0)
  74. if verbose:
  75. print 'Switching back to SGI compatibility'
  76. imgfile.ttob (0)
  77. if verbose: print 'Filtering with "quadratic"'
  78. simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'quadratic')
  79. if verbose: print 'Filtering with "gaussian"'
  80. simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'gaussian', 1.0)
  81. if verbose:
  82. print 'Writing output file'
  83. imgfile.write (outputfile, simage, sizes[0]/2, sizes[1]/2, sizes[2])
  84. os.unlink(outputfile)
  85. def test_main():
  86. uu.decode(findfile('testrgb.uue'), 'test.rgb')
  87. uu.decode(findfile('greyrgb.uue'), 'greytest.rgb')
  88. # Test a 3 byte color image
  89. testimage('test.rgb')
  90. # Test a 1 byte greyscale image
  91. testimage('greytest.rgb')
  92. unlink('test.rgb')
  93. unlink('greytest.rgb')
  94. if __name__ == '__main__':
  95. test_main()