Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +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 | """ |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 8 | from test_support import verbose, unlink |
| 9 | |
| 10 | import imageop, uu |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 11 | |
| 12 | def main(use_rgbimg=1): |
| 13 | |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 14 | # Create binary test files |
| 15 | uu.decode(get_qualified_path('testrgb.uue'), 'test.rgb') |
| 16 | |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 17 | if use_rgbimg: |
| 18 | image, width, height = getrgbimage('test.rgb') |
| 19 | else: |
| 20 | image, width, height = getimage('test.rgb') |
| 21 | |
| 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: |
| 25 | print 'crop' |
| 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: |
| 33 | print 'scale' |
| 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: |
| 41 | print 'tovideo' |
| 42 | videoimage = imageop.tovideo (image, 4, width, height) |
| 43 | |
| 44 | # Convert an rgb image to an 8 bit rgb |
| 45 | if verbose: |
| 46 | print 'rgb2rgb8' |
| 47 | greyimage = imageop.rgb2rgb8(image, width, height) |
| 48 | |
| 49 | # Convert an 8 bit rgb image to a 24 bit rgb image |
| 50 | if verbose: |
| 51 | print 'rgb82rgb' |
| 52 | image = imageop.rgb82rgb(greyimage, width, height) |
| 53 | |
| 54 | # Convert an rgb image to an 8 bit greyscale image |
| 55 | if verbose: |
| 56 | print 'rgb2grey' |
| 57 | greyimage = imageop.rgb2grey(image, width, height) |
| 58 | |
| 59 | # Convert an 8 bit greyscale image to a 24 bit rgb image |
| 60 | if verbose: |
| 61 | print 'grey2rgb' |
| 62 | image = imageop.grey2rgb(greyimage, width, height) |
| 63 | |
| 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: |
| 68 | print 'grey2mono' |
| 69 | 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: |
| 78 | print 'mono2grey' |
| 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: |
| 84 | print 'dither2mono' |
| 85 | 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: |
| 90 | print 'grey2grey4' |
| 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: |
| 96 | print 'grey2grey2' |
| 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: |
| 103 | print 'dither2grey2' |
| 104 | grey2image = imageop.dither2grey2 (greyimage, width, height) |
| 105 | |
| 106 | # Convert a 4-bit greyscale image to an 8-bit greyscale image. |
| 107 | if verbose: |
| 108 | print 'grey42grey' |
| 109 | greyimage = imageop.grey42grey (grey4image, width, height) |
| 110 | |
| 111 | # Convert a 2-bit greyscale image to an 8-bit greyscale image. |
| 112 | if verbose: |
| 113 | print 'grey22grey' |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 114 | image = imageop.grey22grey (grey2image, width, height) |
| 115 | |
| 116 | # Cleanup |
| 117 | unlink('test.rgb') |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 118 | |
| 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: |
| 126 | sizes = rgbimg.sizeofimage(name) |
| 127 | except rgbimg.error: |
| 128 | name = get_qualified_path(name) |
| 129 | sizes = rgbimg.sizeofimage(name) |
| 130 | if verbose: |
| 131 | print 'rgbimg opening test image: %s, sizes: %s' % (name, str(sizes)) |
| 132 | |
| 133 | image = rgbimg.longimagedata(name) |
| 134 | return (image, sizes[0], sizes[1]) |
| 135 | |
| 136 | def getimage(name): |
| 137 | """return a tuple consisting of |
| 138 | image (in 'imgfile' format) width and height |
| 139 | """ |
| 140 | |
| 141 | import imgfile |
| 142 | |
| 143 | try: |
| 144 | sizes = imgfile.getsizes(name) |
| 145 | except imgfile.error: |
| 146 | name = get_qualified_path(name) |
| 147 | sizes = imgfile.getsizes(name) |
| 148 | if verbose: |
| 149 | print 'imgfile opening test image: %s, sizes: %s' % (name, str(sizes)) |
| 150 | |
| 151 | image = imgfile.read(name) |
| 152 | return (image, sizes[0], sizes[1]) |
| 153 | |
| 154 | def get_qualified_path(name): |
Guido van Rossum | f1e6354 | 1997-05-22 20:48:03 +0000 | [diff] [blame] | 155 | """ return a more qualified path to name""" |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 156 | import sys |
| 157 | import os |
Guido van Rossum | a11cccc | 1997-10-06 20:19:59 +0000 | [diff] [blame] | 158 | path = sys.path |
| 159 | try: |
| 160 | path = [os.path.dirname(__file__)] + path |
| 161 | except NameError: |
| 162 | pass |
| 163 | for dir in path: |
Guido van Rossum | f1e6354 | 1997-05-22 20:48:03 +0000 | [diff] [blame] | 164 | fullname = os.path.join(dir, name) |
| 165 | if os.path.exists(fullname): |
| 166 | return fullname |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 167 | return name |
| 168 | |
Guido van Rossum | de554ec | 1997-05-08 23:14:57 +0000 | [diff] [blame] | 169 | # rgbimg (unlike imgfile) is portable to platforms other than SGI. |
| 170 | # So we prefer to use it. |
Guido van Rossum | 228b8e8 | 1997-04-02 06:13:34 +0000 | [diff] [blame] | 171 | main(use_rgbimg=1) |