source: trunk/essentials/dev-lang/python/Lib/test/test_imageop.py@ 3951

Last change on this file since 3951 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 5.7 KB
Line 
1#! /usr/bin/env python
2
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"""
7
8from test.test_support import verbose, unlink
9
10import imageop, uu, os
11
12import warnings
13warnings.filterwarnings("ignore",
14 "the rgbimg module is deprecated",
15 DeprecationWarning,
16 ".*test_imageop")
17
18def main(use_rgbimg=1):
19
20 # Create binary test files
21 uu.decode(get_qualified_path('testrgb'+os.extsep+'uue'), 'test'+os.extsep+'rgb')
22
23 if use_rgbimg:
24 image, width, height = getrgbimage('test'+os.extsep+'rgb')
25 else:
26 image, width, height = getimage('test'+os.extsep+'rgb')
27
28 # Return the selected part of image, which should by width by height
29 # in size and consist of pixels of psize bytes.
30 if verbose:
31 print 'crop'
32 newimage = imageop.crop (image, 4, width, height, 0, 0, 1, 1)
33
34 # Return image scaled to size newwidth by newheight. No interpolation
35 # is done, scaling is done by simple-minded pixel duplication or removal.
36 # Therefore, computer-generated images or dithered images will
37 # not look nice after scaling.
38 if verbose:
39 print 'scale'
40 scaleimage = imageop.scale(image, 4, width, height, 1, 1)
41
42 # Run a vertical low-pass filter over an image. It does so by computing
43 # each destination pixel as the average of two vertically-aligned source
44 # pixels. The main use of this routine is to forestall excessive flicker
45 # if the image two vertically-aligned source pixels, hence the name.
46 if verbose:
47 print 'tovideo'
48 videoimage = imageop.tovideo (image, 4, width, height)
49
50 # Convert an rgb image to an 8 bit rgb
51 if verbose:
52 print 'rgb2rgb8'
53 greyimage = imageop.rgb2rgb8(image, width, height)
54
55 # Convert an 8 bit rgb image to a 24 bit rgb image
56 if verbose:
57 print 'rgb82rgb'
58 image = imageop.rgb82rgb(greyimage, width, height)
59
60 # Convert an rgb image to an 8 bit greyscale image
61 if verbose:
62 print 'rgb2grey'
63 greyimage = imageop.rgb2grey(image, width, height)
64
65 # Convert an 8 bit greyscale image to a 24 bit rgb image
66 if verbose:
67 print 'grey2rgb'
68 image = imageop.grey2rgb(greyimage, width, height)
69
70 # Convert a 8-bit deep greyscale image to a 1-bit deep image by
71 # thresholding all the pixels. The resulting image is tightly packed
72 # and is probably only useful as an argument to mono2grey.
73 if verbose:
74 print 'grey2mono'
75 monoimage = imageop.grey2mono (greyimage, width, height, 0)
76
77 # monoimage, width, height = getimage('monotest.rgb')
78 # Convert a 1-bit monochrome image to an 8 bit greyscale or color image.
79 # All pixels that are zero-valued on input get value p0 on output and
80 # all one-value input pixels get value p1 on output. To convert a
81 # monochrome black-and-white image to greyscale pass the values 0 and
82 # 255 respectively.
83 if verbose:
84 print 'mono2grey'
85 greyimage = imageop.mono2grey (monoimage, width, height, 0, 255)
86
87 # Convert an 8-bit greyscale image to a 1-bit monochrome image using a
88 # (simple-minded) dithering algorithm.
89 if verbose:
90 print 'dither2mono'
91 monoimage = imageop.dither2mono (greyimage, width, height)
92
93 # Convert an 8-bit greyscale image to a 4-bit greyscale image without
94 # dithering.
95 if verbose:
96 print 'grey2grey4'
97 grey4image = imageop.grey2grey4 (greyimage, width, height)
98
99 # Convert an 8-bit greyscale image to a 2-bit greyscale image without
100 # dithering.
101 if verbose:
102 print 'grey2grey2'
103 grey2image = imageop.grey2grey2 (greyimage, width, height)
104
105 # Convert an 8-bit greyscale image to a 2-bit greyscale image with
106 # dithering. As for dither2mono, the dithering algorithm is currently
107 # very simple.
108 if verbose:
109 print 'dither2grey2'
110 grey2image = imageop.dither2grey2 (greyimage, width, height)
111
112 # Convert a 4-bit greyscale image to an 8-bit greyscale image.
113 if verbose:
114 print 'grey42grey'
115 greyimage = imageop.grey42grey (grey4image, width, height)
116
117 # Convert a 2-bit greyscale image to an 8-bit greyscale image.
118 if verbose:
119 print 'grey22grey'
120 image = imageop.grey22grey (grey2image, width, height)
121
122 # Cleanup
123 unlink('test'+os.extsep+'rgb')
124
125def getrgbimage(name):
126 """return a tuple consisting of image (in 'imgfile' format but
127 using rgbimg instead) width and height"""
128
129 import rgbimg
130
131 try:
132 sizes = rgbimg.sizeofimage(name)
133 except rgbimg.error:
134 name = get_qualified_path(name)
135 sizes = rgbimg.sizeofimage(name)
136 if verbose:
137 print 'rgbimg opening test image: %s, sizes: %s' % (name, str(sizes))
138
139 image = rgbimg.longimagedata(name)
140 return (image, sizes[0], sizes[1])
141
142def getimage(name):
143 """return a tuple consisting of
144 image (in 'imgfile' format) width and height
145 """
146
147 import imgfile
148
149 try:
150 sizes = imgfile.getsizes(name)
151 except imgfile.error:
152 name = get_qualified_path(name)
153 sizes = imgfile.getsizes(name)
154 if verbose:
155 print 'imgfile opening test image: %s, sizes: %s' % (name, str(sizes))
156
157 image = imgfile.read(name)
158 return (image, sizes[0], sizes[1])
159
160def get_qualified_path(name):
161 """ return a more qualified path to name"""
162 import sys
163 import os
164 path = sys.path
165 try:
166 path = [os.path.dirname(__file__)] + path
167 except NameError:
168 pass
169 for dir in path:
170 fullname = os.path.join(dir, name)
171 if os.path.exists(fullname):
172 return fullname
173 return name
174
175# rgbimg (unlike imgfile) is portable to platforms other than SGI.
176# So we prefer to use it.
177main(use_rgbimg=1)
Note: See TracBrowser for help on using the repository browser.