Issue #3389: Allow resolving dotted names for handlers in logging configuration files...
[python.git] / Lib / test / test_imageop.py
blob8cd2dc11bd87339510109f74f95c08751e6a8ac3
1 #! /usr/bin/env python
3 """Test script for the imageop module. This has the side
4 effect of partially testing the imgfile module as well.
5 Roger E. Masse
6 """
8 from test.test_support import verbose, unlink, import_module
10 imageop = import_module('imageop', deprecated=True)
11 import uu, os, imgfile
13 def test_main():
15 # Create binary test files
16 uu.decode(get_qualified_path('testrgb'+os.extsep+'uue'), 'test'+os.extsep+'rgb')
18 image, width, height = getimage('test'+os.extsep+'rgb')
20 # Return the selected part of image, which should by width by height
21 # in size and consist of pixels of psize bytes.
22 if verbose:
23 print 'crop'
24 newimage = imageop.crop (image, 4, width, height, 0, 0, 1, 1)
26 # Return image scaled to size newwidth by newheight. No interpolation
27 # is done, scaling is done by simple-minded pixel duplication or removal.
28 # Therefore, computer-generated images or dithered images will
29 # not look nice after scaling.
30 if verbose:
31 print 'scale'
32 scaleimage = imageop.scale(image, 4, width, height, 1, 1)
34 # Run a vertical low-pass filter over an image. It does so by computing
35 # each destination pixel as the average of two vertically-aligned source
36 # pixels. The main use of this routine is to forestall excessive flicker
37 # if the image two vertically-aligned source pixels, hence the name.
38 if verbose:
39 print 'tovideo'
40 videoimage = imageop.tovideo (image, 4, width, height)
42 # Convert an rgb image to an 8 bit rgb
43 if verbose:
44 print 'rgb2rgb8'
45 greyimage = imageop.rgb2rgb8(image, width, height)
47 # Convert an 8 bit rgb image to a 24 bit rgb image
48 if verbose:
49 print 'rgb82rgb'
50 image = imageop.rgb82rgb(greyimage, width, height)
52 # Convert an rgb image to an 8 bit greyscale image
53 if verbose:
54 print 'rgb2grey'
55 greyimage = imageop.rgb2grey(image, width, height)
57 # Convert an 8 bit greyscale image to a 24 bit rgb image
58 if verbose:
59 print 'grey2rgb'
60 image = imageop.grey2rgb(greyimage, width, height)
62 # Convert a 8-bit deep greyscale image to a 1-bit deep image by
63 # thresholding all the pixels. The resulting image is tightly packed
64 # and is probably only useful as an argument to mono2grey.
65 if verbose:
66 print 'grey2mono'
67 monoimage = imageop.grey2mono (greyimage, width, height, 0)
69 # monoimage, width, height = getimage('monotest.rgb')
70 # Convert a 1-bit monochrome image to an 8 bit greyscale or color image.
71 # All pixels that are zero-valued on input get value p0 on output and
72 # all one-value input pixels get value p1 on output. To convert a
73 # monochrome black-and-white image to greyscale pass the values 0 and
74 # 255 respectively.
75 if verbose:
76 print 'mono2grey'
77 greyimage = imageop.mono2grey (monoimage, width, height, 0, 255)
79 # Convert an 8-bit greyscale image to a 1-bit monochrome image using a
80 # (simple-minded) dithering algorithm.
81 if verbose:
82 print 'dither2mono'
83 monoimage = imageop.dither2mono (greyimage, width, height)
85 # Convert an 8-bit greyscale image to a 4-bit greyscale image without
86 # dithering.
87 if verbose:
88 print 'grey2grey4'
89 grey4image = imageop.grey2grey4 (greyimage, width, height)
91 # Convert an 8-bit greyscale image to a 2-bit greyscale image without
92 # dithering.
93 if verbose:
94 print 'grey2grey2'
95 grey2image = imageop.grey2grey2 (greyimage, width, height)
97 # Convert an 8-bit greyscale image to a 2-bit greyscale image with
98 # dithering. As for dither2mono, the dithering algorithm is currently
99 # very simple.
100 if verbose:
101 print 'dither2grey2'
102 grey2image = imageop.dither2grey2 (greyimage, width, height)
104 # Convert a 4-bit greyscale image to an 8-bit greyscale image.
105 if verbose:
106 print 'grey42grey'
107 greyimage = imageop.grey42grey (grey4image, width, height)
109 # Convert a 2-bit greyscale image to an 8-bit greyscale image.
110 if verbose:
111 print 'grey22grey'
112 image = imageop.grey22grey (grey2image, width, height)
114 # Cleanup
115 unlink('test'+os.extsep+'rgb')
117 def getimage(name):
118 """return a tuple consisting of
119 image (in 'imgfile' format) width and height
121 try:
122 sizes = imgfile.getsizes(name)
123 except imgfile.error:
124 name = get_qualified_path(name)
125 sizes = imgfile.getsizes(name)
126 if verbose:
127 print 'imgfile opening test image: %s, sizes: %s' % (name, str(sizes))
129 image = imgfile.read(name)
130 return (image, sizes[0], sizes[1])
132 def get_qualified_path(name):
133 """ return a more qualified path to name"""
134 import sys
135 import os
136 path = sys.path
137 try:
138 path = [os.path.dirname(__file__)] + path
139 except NameError:
140 pass
141 for dir in path:
142 fullname = os.path.join(dir, name)
143 if os.path.exists(fullname):
144 return fullname
145 return name
147 if __name__ == '__main__':
148 test_main()