3 """Test script for the imageop module. This has the side
4 effect of partially testing the imgfile module as well.
8 from test
.test_support
import verbose
, unlink
10 import imageop
, uu
, os
12 def main(use_rgbimg
=1):
14 # Create binary test files
15 uu
.decode(get_qualified_path('testrgb'+os
.extsep
+'uue'), 'test'+os
.extsep
+'rgb')
18 image
, width
, height
= getrgbimage('test'+os
.extsep
+'rgb')
20 image
, width
, height
= getimage('test'+os
.extsep
+'rgb')
22 # Return the selected part of image, which should by width by height
23 # in size and consist of pixels of psize bytes.
26 newimage
= imageop
.crop (image
, 4, width
, height
, 0, 0, 1, 1)
28 # Return image scaled to size newwidth by newheight. No interpolation
29 # is done, scaling is done by simple-minded pixel duplication or removal.
30 # Therefore, computer-generated images or dithered images will
31 # not look nice after scaling.
34 scaleimage
= imageop
.scale(image
, 4, width
, height
, 1, 1)
36 # Run a vertical low-pass filter over an image. It does so by computing
37 # each destination pixel as the average of two vertically-aligned source
38 # pixels. The main use of this routine is to forestall excessive flicker
39 # if the image two vertically-aligned source pixels, hence the name.
42 videoimage
= imageop
.tovideo (image
, 4, width
, height
)
44 # Convert an rgb image to an 8 bit rgb
47 greyimage
= imageop
.rgb2rgb8(image
, width
, height
)
49 # Convert an 8 bit rgb image to a 24 bit rgb image
52 image
= imageop
.rgb82rgb(greyimage
, width
, height
)
54 # Convert an rgb image to an 8 bit greyscale image
57 greyimage
= imageop
.rgb2grey(image
, width
, height
)
59 # Convert an 8 bit greyscale image to a 24 bit rgb image
62 image
= imageop
.grey2rgb(greyimage
, width
, height
)
64 # Convert a 8-bit deep greyscale image to a 1-bit deep image by
65 # thresholding all the pixels. The resulting image is tightly packed
66 # and is probably only useful as an argument to mono2grey.
69 monoimage
= imageop
.grey2mono (greyimage
, width
, height
, 0)
71 # monoimage, width, height = getimage('monotest.rgb')
72 # Convert a 1-bit monochrome image to an 8 bit greyscale or color image.
73 # All pixels that are zero-valued on input get value p0 on output and
74 # all one-value input pixels get value p1 on output. To convert a
75 # monochrome black-and-white image to greyscale pass the values 0 and
79 greyimage
= imageop
.mono2grey (monoimage
, width
, height
, 0, 255)
81 # Convert an 8-bit greyscale image to a 1-bit monochrome image using a
82 # (simple-minded) dithering algorithm.
85 monoimage
= imageop
.dither2mono (greyimage
, width
, height
)
87 # Convert an 8-bit greyscale image to a 4-bit greyscale image without
91 grey4image
= imageop
.grey2grey4 (greyimage
, width
, height
)
93 # Convert an 8-bit greyscale image to a 2-bit greyscale image without
97 grey2image
= imageop
.grey2grey2 (greyimage
, width
, height
)
99 # Convert an 8-bit greyscale image to a 2-bit greyscale image with
100 # dithering. As for dither2mono, the dithering algorithm is currently
104 grey2image
= imageop
.dither2grey2 (greyimage
, width
, height
)
106 # Convert a 4-bit greyscale image to an 8-bit greyscale image.
109 greyimage
= imageop
.grey42grey (grey4image
, width
, height
)
111 # Convert a 2-bit greyscale image to an 8-bit greyscale image.
114 image
= imageop
.grey22grey (grey2image
, width
, height
)
117 unlink('test'+os
.extsep
+'rgb')
119 def getrgbimage(name
):
120 """return a tuple consisting of image (in 'imgfile' format but
121 using rgbimg instead) width and height"""
126 sizes
= rgbimg
.sizeofimage(name
)
128 name
= get_qualified_path(name
)
129 sizes
= rgbimg
.sizeofimage(name
)
131 print 'rgbimg opening test image: %s, sizes: %s' % (name
, str(sizes
))
133 image
= rgbimg
.longimagedata(name
)
134 return (image
, sizes
[0], sizes
[1])
137 """return a tuple consisting of
138 image (in 'imgfile' format) width and height
144 sizes
= imgfile
.getsizes(name
)
145 except imgfile
.error
:
146 name
= get_qualified_path(name
)
147 sizes
= imgfile
.getsizes(name
)
149 print 'imgfile opening test image: %s, sizes: %s' % (name
, str(sizes
))
151 image
= imgfile
.read(name
)
152 return (image
, sizes
[0], sizes
[1])
154 def get_qualified_path(name
):
155 """ return a more qualified path to name"""
160 path
= [os
.path
.dirname(__file__
)] + path
164 fullname
= os
.path
.join(dir, name
)
165 if os
.path
.exists(fullname
):
169 # rgbimg (unlike imgfile) is portable to platforms other than SGI.
170 # So we prefer to use it.