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
, import_module
, run_unittest
10 imageop
= import_module('imageop', deprecated
=True)
11 import uu
, os
, unittest
15 _VALUES
= (1, 2, 2**10, 2**15-1, 2**15, 2**15+1, 2**31-2, 2**31-1)
16 VALUES
= tuple( -x
for x
in reversed(_VALUES
) ) + (0,) + _VALUES
21 class InputValidationTests(unittest
.TestCase
):
23 def _check(self
, name
, size
=None, *extra
):
24 func
= getattr(imageop
, name
)
27 strlen
= abs(width
* height
)
35 arguments
= (data
, size
, width
, height
) + extra
37 arguments
= (data
, width
, height
) + extra
40 except (ValueError, imageop
.error
):
43 def check_size(self
, name
, *extra
):
45 self
._check
(name
, size
, *extra
)
47 def check(self
, name
, *extra
):
48 self
._check
(name
, None, *extra
)
50 def test_input_validation(self
):
51 self
.check_size("crop", 0, 0, 0, 0)
52 self
.check_size("scale", 1, 0)
53 self
.check_size("scale", -1, -1)
54 self
.check_size("tovideo")
55 self
.check("grey2mono", 128)
56 self
.check("grey2grey4")
57 self
.check("grey2grey2")
58 self
.check("dither2mono")
59 self
.check("dither2grey2")
60 self
.check("mono2grey", 0, 0)
61 self
.check("grey22grey")
62 self
.check("rgb2rgb8") # nlen*4 == len
63 self
.check("rgb82rgb")
64 self
.check("rgb2grey")
65 self
.check("grey2rgb")
70 run_unittest(InputValidationTests
)
77 # Create binary test files
78 uu
.decode(get_qualified_path('testrgb'+os
.extsep
+'uue'), 'test'+os
.extsep
+'rgb')
80 image
, width
, height
= getimage('test'+os
.extsep
+'rgb')
82 # Return the selected part of image, which should by width by height
83 # in size and consist of pixels of psize bytes.
86 newimage
= imageop
.crop (image
, 4, width
, height
, 0, 0, 1, 1)
88 # Return image scaled to size newwidth by newheight. No interpolation
89 # is done, scaling is done by simple-minded pixel duplication or removal.
90 # Therefore, computer-generated images or dithered images will
91 # not look nice after scaling.
94 scaleimage
= imageop
.scale(image
, 4, width
, height
, 1, 1)
96 # Run a vertical low-pass filter over an image. It does so by computing
97 # each destination pixel as the average of two vertically-aligned source
98 # pixels. The main use of this routine is to forestall excessive flicker
99 # if the image two vertically-aligned source pixels, hence the name.
102 videoimage
= imageop
.tovideo (image
, 4, width
, height
)
104 # Convert an rgb image to an 8 bit rgb
107 greyimage
= imageop
.rgb2rgb8(image
, width
, height
)
109 # Convert an 8 bit rgb image to a 24 bit rgb image
112 image
= imageop
.rgb82rgb(greyimage
, width
, height
)
114 # Convert an rgb image to an 8 bit greyscale image
117 greyimage
= imageop
.rgb2grey(image
, width
, height
)
119 # Convert an 8 bit greyscale image to a 24 bit rgb image
122 image
= imageop
.grey2rgb(greyimage
, width
, height
)
124 # Convert a 8-bit deep greyscale image to a 1-bit deep image by
125 # thresholding all the pixels. The resulting image is tightly packed
126 # and is probably only useful as an argument to mono2grey.
129 monoimage
= imageop
.grey2mono (greyimage
, width
, height
, 0)
131 # monoimage, width, height = getimage('monotest.rgb')
132 # Convert a 1-bit monochrome image to an 8 bit greyscale or color image.
133 # All pixels that are zero-valued on input get value p0 on output and
134 # all one-value input pixels get value p1 on output. To convert a
135 # monochrome black-and-white image to greyscale pass the values 0 and
139 greyimage
= imageop
.mono2grey (monoimage
, width
, height
, 0, 255)
141 # Convert an 8-bit greyscale image to a 1-bit monochrome image using a
142 # (simple-minded) dithering algorithm.
145 monoimage
= imageop
.dither2mono (greyimage
, width
, height
)
147 # Convert an 8-bit greyscale image to a 4-bit greyscale image without
151 grey4image
= imageop
.grey2grey4 (greyimage
, width
, height
)
153 # Convert an 8-bit greyscale image to a 2-bit greyscale image without
157 grey2image
= imageop
.grey2grey2 (greyimage
, width
, height
)
159 # Convert an 8-bit greyscale image to a 2-bit greyscale image with
160 # dithering. As for dither2mono, the dithering algorithm is currently
164 grey2image
= imageop
.dither2grey2 (greyimage
, width
, height
)
166 # Convert a 4-bit greyscale image to an 8-bit greyscale image.
169 greyimage
= imageop
.grey42grey (grey4image
, width
, height
)
171 # Convert a 2-bit greyscale image to an 8-bit greyscale image.
174 image
= imageop
.grey22grey (grey2image
, width
, height
)
177 unlink('test'+os
.extsep
+'rgb')
180 """return a tuple consisting of
181 image (in 'imgfile' format) width and height
185 sizes
= imgfile
.getsizes(name
)
186 except imgfile
.error
:
187 name
= get_qualified_path(name
)
188 sizes
= imgfile
.getsizes(name
)
190 print 'imgfile opening test image: %s, sizes: %s' % (name
, str(sizes
))
192 image
= imgfile
.read(name
)
193 return (image
, sizes
[0], sizes
[1])
195 def get_qualified_path(name
):
196 """ return a more qualified path to name"""
201 path
= [os
.path
.dirname(__file__
)] + path
205 fullname
= os
.path
.join(dir, name
)
206 if os
.path
.exists(fullname
):
210 if __name__
== '__main__':