Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 7aa9fc5 | 1997-04-16 00:30:45 +0000 | [diff] [blame] | 2 | |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 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 | """ |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 7aa9fc5 | 1997-04-16 00:30:45 +0000 | [diff] [blame] | 8 | from test_support import verbose, unlink |
| 9 | |
| 10 | import imageop, uu |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 11 | |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 12 | def main(use_rgbimg=1): |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 7aa9fc5 | 1997-04-16 00:30:45 +0000 | [diff] [blame] | 14 | # Create binary test files |
| 15 | uu.decode(get_qualified_path('testrgb.uue'), 'test.rgb') |
| 16 | |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 17 | if use_rgbimg: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 18 | image, width, height = getrgbimage('test.rgb') |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 19 | else: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 20 | image, width, height = getimage('test.rgb') |
| 21 | |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 22 | # 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 Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 25 | print 'crop' |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 26 | 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 Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 33 | print 'scale' |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 34 | 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 Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 41 | print 'tovideo' |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 42 | videoimage = imageop.tovideo (image, 4, width, height) |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 43 | |
Roger E. Masse | 52a6ea7 | 1997-01-15 20:58:55 +0000 | [diff] [blame] | 44 | # Convert an rgb image to an 8 bit rgb |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 45 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 46 | print 'rgb2rgb8' |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 47 | 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 Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 51 | print 'rgb82rgb' |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 52 | image = imageop.rgb82rgb(greyimage, width, height) |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 53 | |
Roger E. Masse | 52a6ea7 | 1997-01-15 20:58:55 +0000 | [diff] [blame] | 54 | # Convert an rgb image to an 8 bit greyscale image |
| 55 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 56 | print 'rgb2grey' |
Roger E. Masse | 52a6ea7 | 1997-01-15 20:58:55 +0000 | [diff] [blame] | 57 | 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 Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 61 | print 'grey2rgb' |
Roger E. Masse | 52a6ea7 | 1997-01-15 20:58:55 +0000 | [diff] [blame] | 62 | image = imageop.grey2rgb(greyimage, width, height) |
| 63 | |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 64 | # Convert a 8-bit deep greyscale image to a 1-bit deep image by |
| 65 | # tresholding all the pixels. The resulting image is tightly packed |
| 66 | # and is probably only useful as an argument to mono2grey. |
| 67 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 68 | print 'grey2mono' |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 69 | monoimage = imageop.grey2mono (greyimage, width, height, 0) |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 70 | |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 71 | # monoimage, width, height = getimage('monotest.rgb') |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 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 Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 78 | print 'mono2grey' |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 79 | 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 Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 84 | print 'dither2mono' |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 85 | monoimage = imageop.dither2mono (greyimage, width, height) |
| 86 | |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 87 | # Convert an 8-bit greyscale image to a 4-bit greyscale image without |
| 88 | # dithering. |
| 89 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 90 | print 'grey2grey4' |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 91 | 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 Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 96 | print 'grey2grey2' |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 97 | 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 Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 103 | print 'dither2grey2' |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 104 | 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 Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 108 | print 'grey42grey' |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 109 | 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 Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 113 | print 'grey22grey' |
Guido van Rossum | 7aa9fc5 | 1997-04-16 00:30:45 +0000 | [diff] [blame] | 114 | image = imageop.grey22grey (grey2image, width, height) |
| 115 | |
| 116 | # Cleanup |
| 117 | unlink('test.rgb') |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 118 | |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 119 | def 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 Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 126 | sizes = rgbimg.sizeofimage(name) |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 127 | except rgbimg.error: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 128 | name = get_qualified_path(name) |
| 129 | sizes = rgbimg.sizeofimage(name) |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 130 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 131 | print 'rgbimg opening test image: %s, sizes: %s' % (name, str(sizes)) |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 132 | |
| 133 | image = rgbimg.longimagedata(name) |
| 134 | return (image, sizes[0], sizes[1]) |
| 135 | |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 136 | def getimage(name): |
| 137 | """return a tuple consisting of |
| 138 | image (in 'imgfile' format) width and height |
| 139 | """ |
| 140 | |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 141 | import imgfile |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 142 | |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 143 | try: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 144 | sizes = imgfile.getsizes(name) |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 145 | except imgfile.error: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 146 | name = get_qualified_path(name) |
| 147 | sizes = imgfile.getsizes(name) |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 148 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 149 | print 'imgfile opening test image: %s, sizes: %s' % (name, str(sizes)) |
Roger E. Masse | 47e9830 | 1996-12-20 20:49:06 +0000 | [diff] [blame] | 150 | |
| 151 | image = imgfile.read(name) |
| 152 | return (image, sizes[0], sizes[1]) |
| 153 | |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 154 | def get_qualified_path(name): |
Guido van Rossum | 0ba7e59 | 1997-05-14 18:57:21 +0000 | [diff] [blame] | 155 | """ return a more qualified path to name""" |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 156 | import sys |
| 157 | import os |
Guido van Rossum | f1b0009 | 1997-10-01 22:10:32 +0000 | [diff] [blame] | 158 | path = sys.path |
| 159 | try: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 160 | path = [os.path.dirname(__file__)] + path |
Guido van Rossum | f1b0009 | 1997-10-01 22:10:32 +0000 | [diff] [blame] | 161 | except NameError: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 162 | pass |
Guido van Rossum | f1b0009 | 1997-10-01 22:10:32 +0000 | [diff] [blame] | 163 | for dir in path: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 164 | fullname = os.path.join(dir, name) |
| 165 | if os.path.exists(fullname): |
| 166 | return fullname |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 167 | return name |
| 168 | |
Guido van Rossum | 7aa9fc5 | 1997-04-16 00:30:45 +0000 | [diff] [blame] | 169 | # rgbimg (unlike imgfile) is portable to platforms other than SGI. |
| 170 | # So we prefer to use it. |
Roger E. Masse | e17ec8d | 1997-01-15 20:07:07 +0000 | [diff] [blame] | 171 | main(use_rgbimg=1) |