PyXR

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



0001 #! /usr/bin/env python
0002 
0003 """Test script for the imageop module.  This has the side
0004    effect of partially testing the imgfile module as well.
0005    Roger E. Masse
0006 """
0007 
0008 from test.test_support import verbose, unlink
0009 
0010 import imageop, uu, os
0011 
0012 def main(use_rgbimg=1):
0013 
0014     # Create binary test files
0015     uu.decode(get_qualified_path('testrgb'+os.extsep+'uue'), 'test'+os.extsep+'rgb')
0016 
0017     if use_rgbimg:
0018         image, width, height = getrgbimage('test'+os.extsep+'rgb')
0019     else:
0020         image, width, height = getimage('test'+os.extsep+'rgb')
0021 
0022     # Return the selected part of image, which should by width by height
0023     # in size and consist of pixels of psize bytes.
0024     if verbose:
0025         print 'crop'
0026     newimage = imageop.crop (image, 4, width, height, 0, 0, 1, 1)
0027 
0028     # Return image scaled to size newwidth by newheight. No interpolation
0029     # is done, scaling is done by simple-minded pixel duplication or removal.
0030     # Therefore, computer-generated images or dithered images will
0031     # not look nice after scaling.
0032     if verbose:
0033         print 'scale'
0034     scaleimage = imageop.scale(image, 4, width, height, 1, 1)
0035 
0036     # Run a vertical low-pass filter over an image. It does so by computing
0037     # each destination pixel as the average of two vertically-aligned source
0038     # pixels. The main use of this routine is to forestall excessive flicker
0039     # if the image two vertically-aligned source pixels,  hence the name.
0040     if verbose:
0041         print 'tovideo'
0042     videoimage = imageop.tovideo (image, 4, width, height)
0043 
0044     # Convert an rgb image to an 8 bit rgb
0045     if verbose:
0046         print 'rgb2rgb8'
0047     greyimage = imageop.rgb2rgb8(image, width, height)
0048 
0049     # Convert an 8 bit rgb image to a 24 bit rgb image
0050     if verbose:
0051         print 'rgb82rgb'
0052     image = imageop.rgb82rgb(greyimage, width, height)
0053 
0054     # Convert an rgb image to an 8 bit greyscale image
0055     if verbose:
0056         print 'rgb2grey'
0057     greyimage = imageop.rgb2grey(image, width, height)
0058 
0059     # Convert an 8 bit greyscale image to a 24 bit rgb image
0060     if verbose:
0061         print 'grey2rgb'
0062     image = imageop.grey2rgb(greyimage, width, height)
0063 
0064     # Convert a 8-bit deep greyscale image to a 1-bit deep image by
0065     # thresholding all the pixels. The resulting image is tightly packed
0066     # and is probably only useful as an argument to mono2grey.
0067     if verbose:
0068         print 'grey2mono'
0069     monoimage = imageop.grey2mono (greyimage, width, height, 0)
0070 
0071     # monoimage, width, height = getimage('monotest.rgb')
0072     # Convert a 1-bit monochrome image to an 8 bit greyscale or color image.
0073     # All pixels that are zero-valued on input get value p0 on output and
0074     # all one-value input pixels get value p1 on output. To convert a
0075     # monochrome  black-and-white image to greyscale pass the values 0 and
0076     # 255 respectively.
0077     if verbose:
0078         print 'mono2grey'
0079     greyimage = imageop.mono2grey (monoimage, width, height, 0, 255)
0080 
0081     # Convert an 8-bit greyscale image to a 1-bit monochrome image using a
0082     # (simple-minded) dithering algorithm.
0083     if verbose:
0084         print 'dither2mono'
0085     monoimage = imageop.dither2mono (greyimage, width, height)
0086 
0087     # Convert an 8-bit greyscale image to a 4-bit greyscale image without
0088     # dithering.
0089     if verbose:
0090         print 'grey2grey4'
0091     grey4image = imageop.grey2grey4 (greyimage, width, height)
0092 
0093     # Convert an 8-bit greyscale image to a 2-bit greyscale image without
0094     # dithering.
0095     if verbose:
0096         print 'grey2grey2'
0097     grey2image = imageop.grey2grey2 (greyimage, width, height)
0098 
0099     # Convert an 8-bit greyscale image to a 2-bit greyscale image with
0100     # dithering. As for dither2mono, the dithering algorithm is currently
0101     # very simple.
0102     if verbose:
0103         print 'dither2grey2'
0104     grey2image = imageop.dither2grey2 (greyimage, width, height)
0105 
0106     # Convert a 4-bit greyscale image to an 8-bit greyscale image.
0107     if verbose:
0108         print 'grey42grey'
0109     greyimage = imageop.grey42grey (grey4image, width, height)
0110 
0111     # Convert a 2-bit greyscale image to an 8-bit greyscale image.
0112     if verbose:
0113         print 'grey22grey'
0114     image = imageop.grey22grey (grey2image, width, height)
0115 
0116     # Cleanup
0117     unlink('test'+os.extsep+'rgb')
0118 
0119 def getrgbimage(name):
0120     """return a tuple consisting of image (in 'imgfile' format but
0121     using rgbimg instead) width and height"""
0122 
0123     import rgbimg
0124 
0125     try:
0126         sizes = rgbimg.sizeofimage(name)
0127     except rgbimg.error:
0128         name = get_qualified_path(name)
0129         sizes = rgbimg.sizeofimage(name)
0130     if verbose:
0131         print 'rgbimg opening test image: %s, sizes: %s' % (name, str(sizes))
0132 
0133     image = rgbimg.longimagedata(name)
0134     return (image, sizes[0], sizes[1])
0135 
0136 def getimage(name):
0137     """return a tuple consisting of
0138        image (in 'imgfile' format) width and height
0139     """
0140 
0141     import imgfile
0142 
0143     try:
0144         sizes = imgfile.getsizes(name)
0145     except imgfile.error:
0146         name = get_qualified_path(name)
0147         sizes = imgfile.getsizes(name)
0148     if verbose:
0149         print 'imgfile opening test image: %s, sizes: %s' % (name, str(sizes))
0150 
0151     image = imgfile.read(name)
0152     return (image, sizes[0], sizes[1])
0153 
0154 def get_qualified_path(name):
0155     """ return a more qualified path to name"""
0156     import sys
0157     import os
0158     path = sys.path
0159     try:
0160         path = [os.path.dirname(__file__)] + path
0161     except NameError:
0162         pass
0163     for dir in path:
0164         fullname = os.path.join(dir, name)
0165         if os.path.exists(fullname):
0166             return fullname
0167     return name
0168 
0169 # rgbimg (unlike imgfile) is portable to platforms other than SGI.
0170 # So we prefer to use it.
0171 main(use_rgbimg=1)
0172 

Generated by PyXR 0.9.4
SourceForge.net Logo