1996-12-20 16:49:06 -04:00
|
|
|
#! /usr/bin/env python
|
1997-04-15 21:30:45 -03:00
|
|
|
|
1996-12-20 16:49:06 -04:00
|
|
|
"""Test script for the imageop module. This has the side
|
|
|
|
effect of partially testing the imgfile module as well.
|
|
|
|
Roger E. Masse
|
|
|
|
"""
|
|
|
|
|
2001-01-17 17:51:36 -04:00
|
|
|
from test_support import verbose, unlink
|
1997-04-15 21:30:45 -03:00
|
|
|
|
|
|
|
import imageop, uu
|
1996-12-20 16:49:06 -04:00
|
|
|
|
1997-01-15 16:07:07 -04:00
|
|
|
def main(use_rgbimg=1):
|
1996-12-20 16:49:06 -04:00
|
|
|
|
1997-04-15 21:30:45 -03:00
|
|
|
# Create binary test files
|
|
|
|
uu.decode(get_qualified_path('testrgb.uue'), 'test.rgb')
|
|
|
|
|
1997-01-15 16:07:07 -04:00
|
|
|
if use_rgbimg:
|
1998-03-26 15:42:58 -04:00
|
|
|
image, width, height = getrgbimage('test.rgb')
|
1997-01-15 16:07:07 -04:00
|
|
|
else:
|
1998-03-26 15:42:58 -04:00
|
|
|
image, width, height = getimage('test.rgb')
|
2000-10-23 14:22:08 -03:00
|
|
|
|
1996-12-20 16:49:06 -04:00
|
|
|
# Return the selected part of image, which should by width by height
|
|
|
|
# in size and consist of pixels of psize bytes.
|
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'crop'
|
1996-12-20 16:49:06 -04:00
|
|
|
newimage = imageop.crop (image, 4, width, height, 0, 0, 1, 1)
|
|
|
|
|
|
|
|
# Return image scaled to size newwidth by newheight. No interpolation
|
|
|
|
# is done, scaling is done by simple-minded pixel duplication or removal.
|
|
|
|
# Therefore, computer-generated images or dithered images will
|
2000-10-23 14:22:08 -03:00
|
|
|
# not look nice after scaling.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'scale'
|
1996-12-20 16:49:06 -04:00
|
|
|
scaleimage = imageop.scale(image, 4, width, height, 1, 1)
|
|
|
|
|
|
|
|
# Run a vertical low-pass filter over an image. It does so by computing
|
|
|
|
# each destination pixel as the average of two vertically-aligned source
|
|
|
|
# pixels. The main use of this routine is to forestall excessive flicker
|
2000-10-23 14:22:08 -03:00
|
|
|
# if the image two vertically-aligned source pixels, hence the name.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'tovideo'
|
1996-12-20 16:49:06 -04:00
|
|
|
videoimage = imageop.tovideo (image, 4, width, height)
|
1997-01-15 16:07:07 -04:00
|
|
|
|
1997-01-15 16:58:55 -04:00
|
|
|
# Convert an rgb image to an 8 bit rgb
|
1997-01-15 16:07:07 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'rgb2rgb8'
|
1997-01-15 16:07:07 -04:00
|
|
|
greyimage = imageop.rgb2rgb8(image, width, height)
|
|
|
|
|
|
|
|
# Convert an 8 bit rgb image to a 24 bit rgb image
|
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'rgb82rgb'
|
1997-01-15 16:07:07 -04:00
|
|
|
image = imageop.rgb82rgb(greyimage, width, height)
|
2000-10-23 14:22:08 -03:00
|
|
|
|
1997-01-15 16:58:55 -04:00
|
|
|
# Convert an rgb image to an 8 bit greyscale image
|
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'rgb2grey'
|
1997-01-15 16:58:55 -04:00
|
|
|
greyimage = imageop.rgb2grey(image, width, height)
|
|
|
|
|
|
|
|
# Convert an 8 bit greyscale image to a 24 bit rgb image
|
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'grey2rgb'
|
1997-01-15 16:58:55 -04:00
|
|
|
image = imageop.grey2rgb(greyimage, width, height)
|
2000-10-23 14:22:08 -03:00
|
|
|
|
1996-12-20 16:49:06 -04:00
|
|
|
# Convert a 8-bit deep greyscale image to a 1-bit deep image by
|
2000-07-16 09:04:32 -03:00
|
|
|
# thresholding all the pixels. The resulting image is tightly packed
|
2000-10-23 14:22:08 -03:00
|
|
|
# and is probably only useful as an argument to mono2grey.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'grey2mono'
|
2000-10-23 14:22:08 -03:00
|
|
|
monoimage = imageop.grey2mono (greyimage, width, height, 0)
|
1996-12-20 16:49:06 -04:00
|
|
|
|
1997-01-15 16:07:07 -04:00
|
|
|
# monoimage, width, height = getimage('monotest.rgb')
|
1996-12-20 16:49:06 -04:00
|
|
|
# Convert a 1-bit monochrome image to an 8 bit greyscale or color image.
|
|
|
|
# All pixels that are zero-valued on input get value p0 on output and
|
|
|
|
# all one-value input pixels get value p1 on output. To convert a
|
|
|
|
# monochrome black-and-white image to greyscale pass the values 0 and
|
|
|
|
# 255 respectively.
|
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'mono2grey'
|
1996-12-20 16:49:06 -04:00
|
|
|
greyimage = imageop.mono2grey (monoimage, width, height, 0, 255)
|
|
|
|
|
|
|
|
# Convert an 8-bit greyscale image to a 1-bit monochrome image using a
|
|
|
|
# (simple-minded) dithering algorithm.
|
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'dither2mono'
|
1996-12-20 16:49:06 -04:00
|
|
|
monoimage = imageop.dither2mono (greyimage, width, height)
|
|
|
|
|
|
|
|
# Convert an 8-bit greyscale image to a 4-bit greyscale image without
|
2000-10-23 14:22:08 -03:00
|
|
|
# dithering.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'grey2grey4'
|
2000-10-23 14:22:08 -03:00
|
|
|
grey4image = imageop.grey2grey4 (greyimage, width, height)
|
1996-12-20 16:49:06 -04:00
|
|
|
|
|
|
|
# Convert an 8-bit greyscale image to a 2-bit greyscale image without
|
2000-10-23 14:22:08 -03:00
|
|
|
# dithering.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'grey2grey2'
|
2000-10-23 14:22:08 -03:00
|
|
|
grey2image = imageop.grey2grey2 (greyimage, width, height)
|
1996-12-20 16:49:06 -04:00
|
|
|
|
|
|
|
# Convert an 8-bit greyscale image to a 2-bit greyscale image with
|
|
|
|
# dithering. As for dither2mono, the dithering algorithm is currently
|
2000-10-23 14:22:08 -03:00
|
|
|
# very simple.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'dither2grey2'
|
2000-10-23 14:22:08 -03:00
|
|
|
grey2image = imageop.dither2grey2 (greyimage, width, height)
|
1996-12-20 16:49:06 -04:00
|
|
|
|
2000-10-23 14:22:08 -03:00
|
|
|
# Convert a 4-bit greyscale image to an 8-bit greyscale image.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'grey42grey'
|
2000-10-23 14:22:08 -03:00
|
|
|
greyimage = imageop.grey42grey (grey4image, width, height)
|
1996-12-20 16:49:06 -04:00
|
|
|
|
2000-10-23 14:22:08 -03:00
|
|
|
# Convert a 2-bit greyscale image to an 8-bit greyscale image.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'grey22grey'
|
1997-04-15 21:30:45 -03:00
|
|
|
image = imageop.grey22grey (grey2image, width, height)
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
unlink('test.rgb')
|
1996-12-20 16:49:06 -04:00
|
|
|
|
1997-01-15 16:07:07 -04:00
|
|
|
def getrgbimage(name):
|
|
|
|
"""return a tuple consisting of image (in 'imgfile' format but
|
|
|
|
using rgbimg instead) width and height"""
|
|
|
|
|
|
|
|
import rgbimg
|
|
|
|
|
|
|
|
try:
|
1998-03-26 15:42:58 -04:00
|
|
|
sizes = rgbimg.sizeofimage(name)
|
1997-01-15 16:07:07 -04:00
|
|
|
except rgbimg.error:
|
1998-03-26 15:42:58 -04:00
|
|
|
name = get_qualified_path(name)
|
|
|
|
sizes = rgbimg.sizeofimage(name)
|
1997-01-15 16:07:07 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'rgbimg opening test image: %s, sizes: %s' % (name, str(sizes))
|
1997-01-15 16:07:07 -04:00
|
|
|
|
|
|
|
image = rgbimg.longimagedata(name)
|
|
|
|
return (image, sizes[0], sizes[1])
|
2000-10-23 14:22:08 -03:00
|
|
|
|
1996-12-20 16:49:06 -04:00
|
|
|
def getimage(name):
|
|
|
|
"""return a tuple consisting of
|
|
|
|
image (in 'imgfile' format) width and height
|
|
|
|
"""
|
|
|
|
|
|
|
|
import imgfile
|
2000-10-23 14:22:08 -03:00
|
|
|
|
1996-12-20 16:49:06 -04:00
|
|
|
try:
|
1998-03-26 15:42:58 -04:00
|
|
|
sizes = imgfile.getsizes(name)
|
1996-12-20 16:49:06 -04:00
|
|
|
except imgfile.error:
|
1998-03-26 15:42:58 -04:00
|
|
|
name = get_qualified_path(name)
|
|
|
|
sizes = imgfile.getsizes(name)
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'imgfile opening test image: %s, sizes: %s' % (name, str(sizes))
|
1996-12-20 16:49:06 -04:00
|
|
|
|
|
|
|
image = imgfile.read(name)
|
|
|
|
return (image, sizes[0], sizes[1])
|
|
|
|
|
1997-01-15 16:07:07 -04:00
|
|
|
def get_qualified_path(name):
|
1997-05-14 15:57:21 -03:00
|
|
|
""" return a more qualified path to name"""
|
1997-01-15 16:07:07 -04:00
|
|
|
import sys
|
|
|
|
import os
|
1997-10-01 19:10:32 -03:00
|
|
|
path = sys.path
|
|
|
|
try:
|
1998-03-26 15:42:58 -04:00
|
|
|
path = [os.path.dirname(__file__)] + path
|
1997-10-01 19:10:32 -03:00
|
|
|
except NameError:
|
1998-03-26 15:42:58 -04:00
|
|
|
pass
|
1997-10-01 19:10:32 -03:00
|
|
|
for dir in path:
|
1998-03-26 15:42:58 -04:00
|
|
|
fullname = os.path.join(dir, name)
|
|
|
|
if os.path.exists(fullname):
|
|
|
|
return fullname
|
1997-01-15 16:07:07 -04:00
|
|
|
return name
|
|
|
|
|
1997-04-15 21:30:45 -03:00
|
|
|
# rgbimg (unlike imgfile) is portable to platforms other than SGI.
|
|
|
|
# So we prefer to use it.
|
1997-01-15 16:07:07 -04:00
|
|
|
main(use_rgbimg=1)
|