blob: 6b144c668ed468ae789e79acc724fb5cc1f50b1b [file] [log] [blame]
Guido van Rossum228b8e81997-04-02 06:13:34 +00001#! /usr/bin/env python
Guido van Rossumde554ec1997-05-08 23:14:57 +00002
Guido van Rossum228b8e81997-04-02 06:13:34 +00003"""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"""
Guido van Rossum228b8e81997-04-02 06:13:34 +00007
Guido van Rossumde554ec1997-05-08 23:14:57 +00008from test_support import verbose, unlink
9
10import imageop, uu
Guido van Rossum228b8e81997-04-02 06:13:34 +000011
12def main(use_rgbimg=1):
13
Guido van Rossumde554ec1997-05-08 23:14:57 +000014 # Create binary test files
15 uu.decode(get_qualified_path('testrgb.uue'), 'test.rgb')
16
Guido van Rossum228b8e81997-04-02 06:13:34 +000017 if use_rgbimg:
Guido van Rossum548703a1998-03-26 22:14:20 +000018 image, width, height = getrgbimage('test.rgb')
Guido van Rossum228b8e81997-04-02 06:13:34 +000019 else:
Guido van Rossum548703a1998-03-26 22:14:20 +000020 image, width, height = getimage('test.rgb')
21
Guido van Rossum228b8e81997-04-02 06:13:34 +000022 # Return the selected part of image, which should by width by height
23 # in size and consist of pixels of psize bytes.
24 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +000025 print 'crop'
Guido van Rossum228b8e81997-04-02 06:13:34 +000026 newimage = imageop.crop (image, 4, width, height, 0, 0, 1, 1)
27
28 # Return image scaled to size newwidth by newheight. No interpolation
29 # is done, scaling is done by simple-minded pixel duplication or removal.
30 # Therefore, computer-generated images or dithered images will
31 # not look nice after scaling.
32 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +000033 print 'scale'
Guido van Rossum228b8e81997-04-02 06:13:34 +000034 scaleimage = imageop.scale(image, 4, width, height, 1, 1)
35
36 # Run a vertical low-pass filter over an image. It does so by computing
37 # each destination pixel as the average of two vertically-aligned source
38 # pixels. The main use of this routine is to forestall excessive flicker
39 # if the image two vertically-aligned source pixels, hence the name.
40 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +000041 print 'tovideo'
Guido van Rossum228b8e81997-04-02 06:13:34 +000042 videoimage = imageop.tovideo (image, 4, width, height)
43
44 # Convert an rgb image to an 8 bit rgb
45 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +000046 print 'rgb2rgb8'
Guido van Rossum228b8e81997-04-02 06:13:34 +000047 greyimage = imageop.rgb2rgb8(image, width, height)
48
49 # Convert an 8 bit rgb image to a 24 bit rgb image
50 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +000051 print 'rgb82rgb'
Guido van Rossum228b8e81997-04-02 06:13:34 +000052 image = imageop.rgb82rgb(greyimage, width, height)
53
54 # Convert an rgb image to an 8 bit greyscale image
55 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +000056 print 'rgb2grey'
Guido van Rossum228b8e81997-04-02 06:13:34 +000057 greyimage = imageop.rgb2grey(image, width, height)
58
59 # Convert an 8 bit greyscale image to a 24 bit rgb image
60 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +000061 print 'grey2rgb'
Guido van Rossum228b8e81997-04-02 06:13:34 +000062 image = imageop.grey2rgb(greyimage, width, height)
63
64 # Convert a 8-bit deep greyscale image to a 1-bit deep image by
Thomas Wouters7e474022000-07-16 12:04:32 +000065 # thresholding all the pixels. The resulting image is tightly packed
Guido van Rossum228b8e81997-04-02 06:13:34 +000066 # and is probably only useful as an argument to mono2grey.
67 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +000068 print 'grey2mono'
Guido van Rossum228b8e81997-04-02 06:13:34 +000069 monoimage = imageop.grey2mono (greyimage, width, height, 0)
70
71 # monoimage, width, height = getimage('monotest.rgb')
72 # Convert a 1-bit monochrome image to an 8 bit greyscale or color image.
73 # All pixels that are zero-valued on input get value p0 on output and
74 # all one-value input pixels get value p1 on output. To convert a
75 # monochrome black-and-white image to greyscale pass the values 0 and
76 # 255 respectively.
77 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +000078 print 'mono2grey'
Guido van Rossum228b8e81997-04-02 06:13:34 +000079 greyimage = imageop.mono2grey (monoimage, width, height, 0, 255)
80
81 # Convert an 8-bit greyscale image to a 1-bit monochrome image using a
82 # (simple-minded) dithering algorithm.
83 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +000084 print 'dither2mono'
Guido van Rossum228b8e81997-04-02 06:13:34 +000085 monoimage = imageop.dither2mono (greyimage, width, height)
86
87 # Convert an 8-bit greyscale image to a 4-bit greyscale image without
88 # dithering.
89 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +000090 print 'grey2grey4'
Guido van Rossum228b8e81997-04-02 06:13:34 +000091 grey4image = imageop.grey2grey4 (greyimage, width, height)
92
93 # Convert an 8-bit greyscale image to a 2-bit greyscale image without
94 # dithering.
95 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +000096 print 'grey2grey2'
Guido van Rossum228b8e81997-04-02 06:13:34 +000097 grey2image = imageop.grey2grey2 (greyimage, width, height)
98
99 # Convert an 8-bit greyscale image to a 2-bit greyscale image with
100 # dithering. As for dither2mono, the dithering algorithm is currently
101 # very simple.
102 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +0000103 print 'dither2grey2'
Guido van Rossum228b8e81997-04-02 06:13:34 +0000104 grey2image = imageop.dither2grey2 (greyimage, width, height)
105
106 # Convert a 4-bit greyscale image to an 8-bit greyscale image.
107 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +0000108 print 'grey42grey'
Guido van Rossum228b8e81997-04-02 06:13:34 +0000109 greyimage = imageop.grey42grey (grey4image, width, height)
110
111 # Convert a 2-bit greyscale image to an 8-bit greyscale image.
112 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +0000113 print 'grey22grey'
Guido van Rossumde554ec1997-05-08 23:14:57 +0000114 image = imageop.grey22grey (grey2image, width, height)
115
116 # Cleanup
117 unlink('test.rgb')
Guido van Rossum228b8e81997-04-02 06:13:34 +0000118
119def getrgbimage(name):
120 """return a tuple consisting of image (in 'imgfile' format but
121 using rgbimg instead) width and height"""
122
123 import rgbimg
124
125 try:
Guido van Rossum548703a1998-03-26 22:14:20 +0000126 sizes = rgbimg.sizeofimage(name)
Guido van Rossum228b8e81997-04-02 06:13:34 +0000127 except rgbimg.error:
Guido van Rossum548703a1998-03-26 22:14:20 +0000128 name = get_qualified_path(name)
129 sizes = rgbimg.sizeofimage(name)
Guido van Rossum228b8e81997-04-02 06:13:34 +0000130 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +0000131 print 'rgbimg opening test image: %s, sizes: %s' % (name, str(sizes))
Guido van Rossum228b8e81997-04-02 06:13:34 +0000132
133 image = rgbimg.longimagedata(name)
134 return (image, sizes[0], sizes[1])
135
136def getimage(name):
137 """return a tuple consisting of
138 image (in 'imgfile' format) width and height
139 """
140
141 import imgfile
142
143 try:
Guido van Rossum548703a1998-03-26 22:14:20 +0000144 sizes = imgfile.getsizes(name)
Guido van Rossum228b8e81997-04-02 06:13:34 +0000145 except imgfile.error:
Guido van Rossum548703a1998-03-26 22:14:20 +0000146 name = get_qualified_path(name)
147 sizes = imgfile.getsizes(name)
Guido van Rossum228b8e81997-04-02 06:13:34 +0000148 if verbose:
Guido van Rossum548703a1998-03-26 22:14:20 +0000149 print 'imgfile opening test image: %s, sizes: %s' % (name, str(sizes))
Guido van Rossum228b8e81997-04-02 06:13:34 +0000150
151 image = imgfile.read(name)
152 return (image, sizes[0], sizes[1])
153
154def get_qualified_path(name):
Guido van Rossumf1e63541997-05-22 20:48:03 +0000155 """ return a more qualified path to name"""
Guido van Rossum228b8e81997-04-02 06:13:34 +0000156 import sys
157 import os
Guido van Rossuma11cccc1997-10-06 20:19:59 +0000158 path = sys.path
159 try:
Guido van Rossum548703a1998-03-26 22:14:20 +0000160 path = [os.path.dirname(__file__)] + path
Guido van Rossuma11cccc1997-10-06 20:19:59 +0000161 except NameError:
Guido van Rossum548703a1998-03-26 22:14:20 +0000162 pass
Guido van Rossuma11cccc1997-10-06 20:19:59 +0000163 for dir in path:
Guido van Rossum548703a1998-03-26 22:14:20 +0000164 fullname = os.path.join(dir, name)
165 if os.path.exists(fullname):
166 return fullname
Guido van Rossum228b8e81997-04-02 06:13:34 +0000167 return name
168
Guido van Rossumde554ec1997-05-08 23:14:57 +0000169# rgbimg (unlike imgfile) is portable to platforms other than SGI.
170# So we prefer to use it.
Guido van Rossum228b8e81997-04-02 06:13:34 +0000171main(use_rgbimg=1)