blob: ff83c45e53c45706970abd08e6a29da1429e3986 [file] [log] [blame]
Roger E. Masse47e98301996-12-20 20:49:06 +00001#! /usr/bin/env python
Guido van Rossum7aa9fc51997-04-16 00:30:45 +00002
Roger E. Masse47e98301996-12-20 20:49:06 +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"""
Roger E. Masse47e98301996-12-20 20:49:06 +00007
Fredrik Lundhf7850422001-01-17 21:51:36 +00008from test_support import verbose, unlink
Guido van Rossum7aa9fc51997-04-16 00:30:45 +00009
Guido van Rossume2ae77b2001-10-24 20:42:55 +000010import imageop, uu, os
Roger E. Masse47e98301996-12-20 20:49:06 +000011
Roger E. Massee17ec8d1997-01-15 20:07:07 +000012def main(use_rgbimg=1):
Roger E. Masse47e98301996-12-20 20:49:06 +000013
Guido van Rossum7aa9fc51997-04-16 00:30:45 +000014 # Create binary test files
Guido van Rossume2ae77b2001-10-24 20:42:55 +000015 uu.decode(get_qualified_path('testrgb'+os.extsep+'uue'), 'test'+os.extsep+'rgb')
Guido van Rossum7aa9fc51997-04-16 00:30:45 +000016
Roger E. Massee17ec8d1997-01-15 20:07:07 +000017 if use_rgbimg:
Guido van Rossume2ae77b2001-10-24 20:42:55 +000018 image, width, height = getrgbimage('test'+os.extsep+'rgb')
Roger E. Massee17ec8d1997-01-15 20:07:07 +000019 else:
Guido van Rossume2ae77b2001-10-24 20:42:55 +000020 image, width, height = getimage('test'+os.extsep+'rgb')
Fred Drake004d5e62000-10-23 17:22:08 +000021
Roger E. Masse47e98301996-12-20 20:49:06 +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 Rossum41360a41998-03-26 19:42:58 +000025 print 'crop'
Roger E. Masse47e98301996-12-20 20:49:06 +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
Fred Drake004d5e62000-10-23 17:22:08 +000031 # not look nice after scaling.
Roger E. Masse47e98301996-12-20 20:49:06 +000032 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000033 print 'scale'
Roger E. Masse47e98301996-12-20 20:49:06 +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
Fred Drake004d5e62000-10-23 17:22:08 +000039 # if the image two vertically-aligned source pixels, hence the name.
Roger E. Masse47e98301996-12-20 20:49:06 +000040 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000041 print 'tovideo'
Roger E. Masse47e98301996-12-20 20:49:06 +000042 videoimage = imageop.tovideo (image, 4, width, height)
Roger E. Massee17ec8d1997-01-15 20:07:07 +000043
Roger E. Masse52a6ea71997-01-15 20:58:55 +000044 # Convert an rgb image to an 8 bit rgb
Roger E. Massee17ec8d1997-01-15 20:07:07 +000045 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000046 print 'rgb2rgb8'
Roger E. Massee17ec8d1997-01-15 20:07:07 +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 Rossum41360a41998-03-26 19:42:58 +000051 print 'rgb82rgb'
Roger E. Massee17ec8d1997-01-15 20:07:07 +000052 image = imageop.rgb82rgb(greyimage, width, height)
Fred Drake004d5e62000-10-23 17:22:08 +000053
Roger E. Masse52a6ea71997-01-15 20:58:55 +000054 # Convert an rgb image to an 8 bit greyscale image
55 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000056 print 'rgb2grey'
Roger E. Masse52a6ea71997-01-15 20:58:55 +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 Rossum41360a41998-03-26 19:42:58 +000061 print 'grey2rgb'
Roger E. Masse52a6ea71997-01-15 20:58:55 +000062 image = imageop.grey2rgb(greyimage, width, height)
Fred Drake004d5e62000-10-23 17:22:08 +000063
Roger E. Masse47e98301996-12-20 20:49:06 +000064 # 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
Fred Drake004d5e62000-10-23 17:22:08 +000066 # and is probably only useful as an argument to mono2grey.
Roger E. Masse47e98301996-12-20 20:49:06 +000067 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000068 print 'grey2mono'
Fred Drake004d5e62000-10-23 17:22:08 +000069 monoimage = imageop.grey2mono (greyimage, width, height, 0)
Roger E. Masse47e98301996-12-20 20:49:06 +000070
Roger E. Massee17ec8d1997-01-15 20:07:07 +000071 # monoimage, width, height = getimage('monotest.rgb')
Roger E. Masse47e98301996-12-20 20:49:06 +000072 # 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 Rossum41360a41998-03-26 19:42:58 +000078 print 'mono2grey'
Roger E. Masse47e98301996-12-20 20:49:06 +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 Rossum41360a41998-03-26 19:42:58 +000084 print 'dither2mono'
Roger E. Masse47e98301996-12-20 20:49:06 +000085 monoimage = imageop.dither2mono (greyimage, width, height)
86
Roger E. Masse47e98301996-12-20 20:49:06 +000087 # Convert an 8-bit greyscale image to a 4-bit greyscale image without
Fred Drake004d5e62000-10-23 17:22:08 +000088 # dithering.
Roger E. Masse47e98301996-12-20 20:49:06 +000089 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000090 print 'grey2grey4'
Fred Drake004d5e62000-10-23 17:22:08 +000091 grey4image = imageop.grey2grey4 (greyimage, width, height)
Roger E. Masse47e98301996-12-20 20:49:06 +000092
93 # Convert an 8-bit greyscale image to a 2-bit greyscale image without
Fred Drake004d5e62000-10-23 17:22:08 +000094 # dithering.
Roger E. Masse47e98301996-12-20 20:49:06 +000095 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000096 print 'grey2grey2'
Fred Drake004d5e62000-10-23 17:22:08 +000097 grey2image = imageop.grey2grey2 (greyimage, width, height)
Roger E. Masse47e98301996-12-20 20:49:06 +000098
99 # Convert an 8-bit greyscale image to a 2-bit greyscale image with
100 # dithering. As for dither2mono, the dithering algorithm is currently
Fred Drake004d5e62000-10-23 17:22:08 +0000101 # very simple.
Roger E. Masse47e98301996-12-20 20:49:06 +0000102 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000103 print 'dither2grey2'
Fred Drake004d5e62000-10-23 17:22:08 +0000104 grey2image = imageop.dither2grey2 (greyimage, width, height)
Roger E. Masse47e98301996-12-20 20:49:06 +0000105
Fred Drake004d5e62000-10-23 17:22:08 +0000106 # Convert a 4-bit greyscale image to an 8-bit greyscale image.
Roger E. Masse47e98301996-12-20 20:49:06 +0000107 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000108 print 'grey42grey'
Fred Drake004d5e62000-10-23 17:22:08 +0000109 greyimage = imageop.grey42grey (grey4image, width, height)
Roger E. Masse47e98301996-12-20 20:49:06 +0000110
Fred Drake004d5e62000-10-23 17:22:08 +0000111 # Convert a 2-bit greyscale image to an 8-bit greyscale image.
Roger E. Masse47e98301996-12-20 20:49:06 +0000112 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000113 print 'grey22grey'
Guido van Rossum7aa9fc51997-04-16 00:30:45 +0000114 image = imageop.grey22grey (grey2image, width, height)
115
116 # Cleanup
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000117 unlink('test'+os.extsep+'rgb')
Roger E. Masse47e98301996-12-20 20:49:06 +0000118
Roger E. Massee17ec8d1997-01-15 20:07:07 +0000119def 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 Rossum41360a41998-03-26 19:42:58 +0000126 sizes = rgbimg.sizeofimage(name)
Roger E. Massee17ec8d1997-01-15 20:07:07 +0000127 except rgbimg.error:
Guido van Rossum41360a41998-03-26 19:42:58 +0000128 name = get_qualified_path(name)
129 sizes = rgbimg.sizeofimage(name)
Roger E. Massee17ec8d1997-01-15 20:07:07 +0000130 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000131 print 'rgbimg opening test image: %s, sizes: %s' % (name, str(sizes))
Roger E. Massee17ec8d1997-01-15 20:07:07 +0000132
133 image = rgbimg.longimagedata(name)
134 return (image, sizes[0], sizes[1])
Fred Drake004d5e62000-10-23 17:22:08 +0000135
Roger E. Masse47e98301996-12-20 20:49:06 +0000136def getimage(name):
137 """return a tuple consisting of
138 image (in 'imgfile' format) width and height
139 """
140
Roger E. Masse47e98301996-12-20 20:49:06 +0000141 import imgfile
Fred Drake004d5e62000-10-23 17:22:08 +0000142
Roger E. Masse47e98301996-12-20 20:49:06 +0000143 try:
Guido van Rossum41360a41998-03-26 19:42:58 +0000144 sizes = imgfile.getsizes(name)
Roger E. Masse47e98301996-12-20 20:49:06 +0000145 except imgfile.error:
Guido van Rossum41360a41998-03-26 19:42:58 +0000146 name = get_qualified_path(name)
147 sizes = imgfile.getsizes(name)
Roger E. Masse47e98301996-12-20 20:49:06 +0000148 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000149 print 'imgfile opening test image: %s, sizes: %s' % (name, str(sizes))
Roger E. Masse47e98301996-12-20 20:49:06 +0000150
151 image = imgfile.read(name)
152 return (image, sizes[0], sizes[1])
153
Roger E. Massee17ec8d1997-01-15 20:07:07 +0000154def get_qualified_path(name):
Guido van Rossum0ba7e591997-05-14 18:57:21 +0000155 """ return a more qualified path to name"""
Roger E. Massee17ec8d1997-01-15 20:07:07 +0000156 import sys
157 import os
Guido van Rossumf1b00091997-10-01 22:10:32 +0000158 path = sys.path
159 try:
Guido van Rossum41360a41998-03-26 19:42:58 +0000160 path = [os.path.dirname(__file__)] + path
Guido van Rossumf1b00091997-10-01 22:10:32 +0000161 except NameError:
Guido van Rossum41360a41998-03-26 19:42:58 +0000162 pass
Guido van Rossumf1b00091997-10-01 22:10:32 +0000163 for dir in path:
Guido van Rossum41360a41998-03-26 19:42:58 +0000164 fullname = os.path.join(dir, name)
165 if os.path.exists(fullname):
166 return fullname
Roger E. Massee17ec8d1997-01-15 20:07:07 +0000167 return name
168
Guido van Rossum7aa9fc51997-04-16 00:30:45 +0000169# rgbimg (unlike imgfile) is portable to platforms other than SGI.
170# So we prefer to use it.
Roger E. Massee17ec8d1997-01-15 20:07:07 +0000171main(use_rgbimg=1)