blob: 54bae1a70544aff5c15807263da01e1d9c8d441d [file] [log] [blame]
Roger E. Masse47e98301996-12-20 20:49:06 +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():
11
12 image, width, height = getimage('test.rgb')
13
14 # Return the selected part of image, which should by width by height
15 # in size and consist of pixels of psize bytes.
16 if verbose:
17 print 'crop'
18 newimage = imageop.crop (image, 4, width, height, 0, 0, 1, 1)
19
20 # Return image scaled to size newwidth by newheight. No interpolation
21 # is done, scaling is done by simple-minded pixel duplication or removal.
22 # Therefore, computer-generated images or dithered images will
23 # not look nice after scaling.
24 if verbose:
25 print 'scale'
26 scaleimage = imageop.scale(image, 4, width, height, 1, 1)
27
28 # Run a vertical low-pass filter over an image. It does so by computing
29 # each destination pixel as the average of two vertically-aligned source
30 # pixels. The main use of this routine is to forestall excessive flicker
31 # if the image two vertically-aligned source pixels, hence the name.
32 if verbose:
33 print 'tovideo'
34 videoimage = imageop.tovideo (image, 4, width, height)
35
36 image, width, height = getimage('greytest.rgb')
37 # Convert a 8-bit deep greyscale image to a 1-bit deep image by
38 # tresholding all the pixels. The resulting image is tightly packed
39 # and is probably only useful as an argument to mono2grey.
40 if verbose:
41 print 'grey2mono'
42 monoimage = imageop.grey2mono (image, width, height, 0)
43
44 #monoimage, width, height = getimage('monotest.rgb')
45
46 # Convert a 1-bit monochrome image to an 8 bit greyscale or color image.
47 # All pixels that are zero-valued on input get value p0 on output and
48 # all one-value input pixels get value p1 on output. To convert a
49 # monochrome black-and-white image to greyscale pass the values 0 and
50 # 255 respectively.
51 if verbose:
52 print 'mono2grey'
53 greyimage = imageop.mono2grey (monoimage, width, height, 0, 255)
54
55 # Convert an 8-bit greyscale image to a 1-bit monochrome image using a
56 # (simple-minded) dithering algorithm.
57 if verbose:
58 print 'dither2mono'
59 monoimage = imageop.dither2mono (greyimage, width, height)
60
61
62 # Convert an 8-bit greyscale image to a 4-bit greyscale image without
63 # dithering.
64 if verbose:
65 print 'grey2grey4'
66 grey4image = imageop.grey2grey4 (greyimage, width, height)
67
68 # Convert an 8-bit greyscale image to a 2-bit greyscale image without
69 # dithering.
70 if verbose:
71 print 'grey2grey2'
72 grey2image = imageop.grey2grey2 (greyimage, width, height)
73
74 # Convert an 8-bit greyscale image to a 2-bit greyscale image with
75 # dithering. As for dither2mono, the dithering algorithm is currently
76 # very simple.
77 if verbose:
78 print 'dither2grey2'
79 grey2image = imageop.dither2grey2 (greyimage, width, height)
80
81 # Convert a 4-bit greyscale image to an 8-bit greyscale image.
82 if verbose:
83 print 'grey42grey'
84 greyimage = imageop.grey42grey (grey4image, width, height)
85
86 # Convert a 2-bit greyscale image to an 8-bit greyscale image.
87 if verbose:
88 print 'grey22grey'
89 image = imageop.grey22grey (grey2image, width, height)
90
91def getimage(name):
92 """return a tuple consisting of
93 image (in 'imgfile' format) width and height
94 """
95
96 import sys
97 import os
98 import imgfile
99 import string
100
101 # try opening the name directly
102 try:
103 sizes = imgfile.getsizes(name)
104 except imgfile.error:
105 # get a more qualified path component of the script...
106 if __name__ == '__main__':
107 ourname = sys.argv[0]
108 else: # ...or the full path of the module
109 ourname = sys.modules[__name__].__file__
110
111 parts = string.splitfields(ourname, os.sep)
112 parts[-1] = name
113 name = string.joinfields(parts, os.sep)
114 sizes = imgfile.getsizes(name)
115 if verbose:
116 print 'Opening test image: %s, sizes: %s' % (name, str(sizes))
117
118 image = imgfile.read(name)
119 return (image, sizes[0], sizes[1])
120
121main()