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