[Image-SIG] Code for testing "convert"

Edward C. Jones edcjones@erols.com
Wed, 25 Dec 2002 10:57:45 -0500


#! /usr/bin/env python
"""Test "convert" for all combinations of modes"""

import Image

modes = ('1', 'L', 'RGB', 'RGBA', 'RGBX', 'CMYK', 'YCbCr', 'I', 'F')

values = { '1' : (0, 1),
            'L' : (0, 255),
            'I' : (-1024, -64, 0, 64, 1024),
            'F' : (-1.0e15, -100000.0, -1024.0, -64.0, 0.0,
                   64.0, 1024.0, 1.0e5, float(2L**30), float(2L**33),
                   1.0e15),
            'RGB' : ((0, 0, 0), (255, 255, 255)),
            'RGBA' : ((0, 0, 0, 0), (255, 255, 255, 255)),
            'RGBX' : ((0, 0, 0, 0), (255, 255, 255, 255)),
            'CMYK' : ((0, 0, 0, 0), (255, 255, 255, 255)),
            'YCbCr' : ((0, 0, 0, 0), (255, 255, 255, 255)) }

text = \
"""Conversions to mode "1" are often buggy because of problems with the
internal representation of mode "1". The expression
     Image.new("1", (1,1), 255).getpixel((0,0))
returns %i. I use Gentoo Linux 1.3 on a PC with gcc 2.95.3.
"""
print text % Image.new('1', (1,1), 255).getpixel((0,0))

for mode1 in modes:
     im = Image.new(mode1, (1,1))
     for value in values[mode1]:
         print 'mode "%s", value %s to mode' % (mode1, str(value))
         im.putpixel((0,0), value)
         for mode2 in modes:
             im2 = im.convert(mode2)
             v = im2.getpixel((0,0))
             s = '"%s"' % mode2
             print '   %-7s %s' % (s, str(v))
         print
     print '--------------------------------'
     print

# Ed Jones