1997-04-02 02:13:34 -04:00
|
|
|
#! /usr/bin/env python
|
1997-05-08 20:14:57 -03:00
|
|
|
|
1997-04-02 02:13:34 -04:00
|
|
|
"""Test script for the imageop module. This has the side
|
|
|
|
effect of partially testing the imgfile module as well.
|
|
|
|
Roger E. Masse
|
|
|
|
"""
|
|
|
|
|
1997-05-08 20:14:57 -03:00
|
|
|
from test_support import verbose, unlink
|
|
|
|
|
|
|
|
import imageop, uu
|
1997-04-02 02:13:34 -04:00
|
|
|
|
|
|
|
def main(use_rgbimg=1):
|
|
|
|
|
1997-05-08 20:14:57 -03:00
|
|
|
# Create binary test files
|
|
|
|
uu.decode(get_qualified_path('testrgb.uue'), 'test.rgb')
|
|
|
|
|
1997-04-02 02:13:34 -04:00
|
|
|
if use_rgbimg:
|
1998-03-26 18:14:20 -04:00
|
|
|
image, width, height = getrgbimage('test.rgb')
|
1997-04-02 02:13:34 -04:00
|
|
|
else:
|
1998-03-26 18:14:20 -04:00
|
|
|
image, width, height = getimage('test.rgb')
|
|
|
|
|
1997-04-02 02:13:34 -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 18:14:20 -04:00
|
|
|
print 'crop'
|
1997-04-02 02:13:34 -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
|
|
|
|
# not look nice after scaling.
|
|
|
|
if verbose:
|
1998-03-26 18:14:20 -04:00
|
|
|
print 'scale'
|
1997-04-02 02:13:34 -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
|
|
|
|
# if the image two vertically-aligned source pixels, hence the name.
|
|
|
|
if verbose:
|
1998-03-26 18:14:20 -04:00
|
|
|
print 'tovideo'
|
1997-04-02 02:13:34 -04:00
|
|
|
videoimage = imageop.tovideo (image, 4, width, height)
|
|
|
|
|
|
|
|
# Convert an rgb image to an 8 bit rgb
|
|
|
|
if verbose:
|
1998-03-26 18:14:20 -04:00
|
|
|
print 'rgb2rgb8'
|
1997-04-02 02:13:34 -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 18:14:20 -04:00
|
|
|
print 'rgb82rgb'
|
1997-04-02 02:13:34 -04:00
|
|
|
image = imageop.rgb82rgb(greyimage, width, height)
|
|
|
|
|
|
|
|
# Convert an rgb image to an 8 bit greyscale image
|
|
|
|
if verbose:
|
1998-03-26 18:14:20 -04:00
|
|
|
print 'rgb2grey'
|
1997-04-02 02:13:34 -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 18:14:20 -04:00
|
|
|
print 'grey2rgb'
|
1997-04-02 02:13:34 -04:00
|
|
|
image = imageop.grey2rgb(greyimage, width, height)
|
|
|
|
|
|
|
|
# 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
|
1997-04-02 02:13:34 -04:00
|
|
|
# and is probably only useful as an argument to mono2grey.
|
|
|
|
if verbose:
|
1998-03-26 18:14:20 -04:00
|
|
|
print 'grey2mono'
|
1997-04-02 02:13:34 -04:00
|
|
|
monoimage = imageop.grey2mono (greyimage, width, height, 0)
|
|
|
|
|
|
|
|
# monoimage, width, height = getimage('monotest.rgb')
|
|
|
|
# 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 18:14:20 -04:00
|
|
|
print 'mono2grey'
|
1997-04-02 02:13:34 -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 18:14:20 -04:00
|
|
|
print 'dither2mono'
|
1997-04-02 02:13:34 -04:00
|
|
|
monoimage = imageop.dither2mono (greyimage, width, height)
|
|
|
|
|
|
|
|
# Convert an 8-bit greyscale image to a 4-bit greyscale image without
|
|
|
|
# dithering.
|
|
|
|
if verbose:
|
1998-03-26 18:14:20 -04:00
|
|
|
print 'grey2grey4'
|
1997-04-02 02:13:34 -04:00
|
|
|
grey4image = imageop.grey2grey4 (greyimage, width, height)
|
|
|
|
|
|
|
|
# Convert an 8-bit greyscale image to a 2-bit greyscale image without
|
|
|
|
# dithering.
|
|
|
|
if verbose:
|
1998-03-26 18:14:20 -04:00
|
|
|
print 'grey2grey2'
|
1997-04-02 02:13:34 -04:00
|
|
|
grey2image = imageop.grey2grey2 (greyimage, width, height)
|
|
|
|
|
|
|
|
# Convert an 8-bit greyscale image to a 2-bit greyscale image with
|
|
|
|
# dithering. As for dither2mono, the dithering algorithm is currently
|
|
|
|
# very simple.
|
|
|
|
if verbose:
|
1998-03-26 18:14:20 -04:00
|
|
|
print 'dither2grey2'
|
1997-04-02 02:13:34 -04:00
|
|
|
grey2image = imageop.dither2grey2 (greyimage, width, height)
|
|
|
|
|
|
|
|
# Convert a 4-bit greyscale image to an 8-bit greyscale image.
|
|
|
|
if verbose:
|
1998-03-26 18:14:20 -04:00
|
|
|
print 'grey42grey'
|
1997-04-02 02:13:34 -04:00
|
|
|
greyimage = imageop.grey42grey (grey4image, width, height)
|
|
|
|
|
|
|
|
# Convert a 2-bit greyscale image to an 8-bit greyscale image.
|
|
|
|
if verbose:
|
1998-03-26 18:14:20 -04:00
|
|
|
print 'grey22grey'
|
1997-05-08 20:14:57 -03:00
|
|
|
image = imageop.grey22grey (grey2image, width, height)
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
unlink('test.rgb')
|
1997-04-02 02:13:34 -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 18:14:20 -04:00
|
|
|
sizes = rgbimg.sizeofimage(name)
|
1997-04-02 02:13:34 -04:00
|
|
|
except rgbimg.error:
|
1998-03-26 18:14:20 -04:00
|
|
|
name = get_qualified_path(name)
|
|
|
|
sizes = rgbimg.sizeofimage(name)
|
1997-04-02 02:13:34 -04:00
|
|
|
if verbose:
|
1998-03-26 18:14:20 -04:00
|
|
|
print 'rgbimg opening test image: %s, sizes: %s' % (name, str(sizes))
|
1997-04-02 02:13:34 -04:00
|
|
|
|
|
|
|
image = rgbimg.longimagedata(name)
|
|
|
|
return (image, sizes[0], sizes[1])
|
|
|
|
|
|
|
|
def getimage(name):
|
|
|
|
"""return a tuple consisting of
|
|
|
|
image (in 'imgfile' format) width and height
|
|
|
|
"""
|
|
|
|
|
|
|
|
import imgfile
|
|
|
|
|
|
|
|
try:
|
1998-03-26 18:14:20 -04:00
|
|
|
sizes = imgfile.getsizes(name)
|
1997-04-02 02:13:34 -04:00
|
|
|
except imgfile.error:
|
1998-03-26 18:14:20 -04:00
|
|
|
name = get_qualified_path(name)
|
|
|
|
sizes = imgfile.getsizes(name)
|
1997-04-02 02:13:34 -04:00
|
|
|
if verbose:
|
1998-03-26 18:14:20 -04:00
|
|
|
print 'imgfile opening test image: %s, sizes: %s' % (name, str(sizes))
|
1997-04-02 02:13:34 -04:00
|
|
|
|
|
|
|
image = imgfile.read(name)
|
|
|
|
return (image, sizes[0], sizes[1])
|
|
|
|
|
|
|
|
def get_qualified_path(name):
|
1997-05-22 17:48:03 -03:00
|
|
|
""" return a more qualified path to name"""
|
1997-04-02 02:13:34 -04:00
|
|
|
import sys
|
|
|
|
import os
|
1997-10-06 17:19:59 -03:00
|
|
|
path = sys.path
|
|
|
|
try:
|
1998-03-26 18:14:20 -04:00
|
|
|
path = [os.path.dirname(__file__)] + path
|
1997-10-06 17:19:59 -03:00
|
|
|
except NameError:
|
1998-03-26 18:14:20 -04:00
|
|
|
pass
|
1997-10-06 17:19:59 -03:00
|
|
|
for dir in path:
|
1998-03-26 18:14:20 -04:00
|
|
|
fullname = os.path.join(dir, name)
|
|
|
|
if os.path.exists(fullname):
|
|
|
|
return fullname
|
1997-04-02 02:13:34 -04:00
|
|
|
return name
|
|
|
|
|
1997-05-08 20:14:57 -03:00
|
|
|
# rgbimg (unlike imgfile) is portable to platforms other than SGI.
|
|
|
|
# So we prefer to use it.
|
1997-04-02 02:13:34 -04:00
|
|
|
main(use_rgbimg=1)
|