mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 01:47:47 +03:00 
			
		
		
		
	Convert more tests
This commit is contained in:
		
							parent
							
								
									07aa1a56bb
								
							
						
					
					
						commit
						8a870c0ee9
					
				| 
						 | 
					@ -1,86 +0,0 @@
 | 
				
			||||||
from tester import *
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from PIL import Image
 | 
					 | 
				
			||||||
import os
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
base = os.path.join('Tests', 'images', 'bmp')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def get_files(d, ext='.bmp'):
 | 
					 | 
				
			||||||
    return [os.path.join(base,d,f) for f
 | 
					 | 
				
			||||||
            in os.listdir(os.path.join(base, d)) if ext in f]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test_bad():
 | 
					 | 
				
			||||||
    """ These shouldn't crash/dos, but they shouldn't return anything either """
 | 
					 | 
				
			||||||
    for f in get_files('b'):
 | 
					 | 
				
			||||||
        try:
 | 
					 | 
				
			||||||
            im = Image.open(f)
 | 
					 | 
				
			||||||
            im.load()
 | 
					 | 
				
			||||||
        except Exception as msg:
 | 
					 | 
				
			||||||
            pass
 | 
					 | 
				
			||||||
            # print ("Bad Image %s: %s" %(f,msg))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test_questionable():
 | 
					 | 
				
			||||||
    """ These shouldn't crash/dos, but its not well defined that these are in spec """
 | 
					 | 
				
			||||||
    for f in get_files('q'):
 | 
					 | 
				
			||||||
        try:
 | 
					 | 
				
			||||||
            im = Image.open(f)
 | 
					 | 
				
			||||||
            im.load()
 | 
					 | 
				
			||||||
        except Exception as msg:
 | 
					 | 
				
			||||||
            pass
 | 
					 | 
				
			||||||
            # print ("Bad Image %s: %s" %(f,msg))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test_good():
 | 
					 | 
				
			||||||
    """ These should all work. There's a set of target files in the
 | 
					 | 
				
			||||||
    html directory that we can compare against. """
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # Target files, if they're not just replacing the extension
 | 
					 | 
				
			||||||
    file_map = { 'pal1wb.bmp': 'pal1.png',
 | 
					 | 
				
			||||||
                 'pal4rle.bmp': 'pal4.png', 
 | 
					 | 
				
			||||||
                 'pal8-0.bmp': 'pal8.png',
 | 
					 | 
				
			||||||
                 'pal8rle.bmp': 'pal8.png',
 | 
					 | 
				
			||||||
                 'pal8topdown.bmp': 'pal8.png',
 | 
					 | 
				
			||||||
                 'pal8nonsquare.bmp': 'pal8nonsquare-v.png',
 | 
					 | 
				
			||||||
                 'pal8os2.bmp': 'pal8.png',
 | 
					 | 
				
			||||||
                 'pal8os2sp.bmp': 'pal8.png',
 | 
					 | 
				
			||||||
                 'pal8os2v2.bmp': 'pal8.png',
 | 
					 | 
				
			||||||
                 'pal8os2v2-16.bmp': 'pal8.png',
 | 
					 | 
				
			||||||
                 'pal8v4.bmp': 'pal8.png',
 | 
					 | 
				
			||||||
                 'pal8v5.bmp': 'pal8.png',
 | 
					 | 
				
			||||||
                 'rgb16-565pal.bmp': 'rgb16-565.png',
 | 
					 | 
				
			||||||
                 'rgb24pal.bmp': 'rgb24.png',
 | 
					 | 
				
			||||||
                 'rgb32.bmp': 'rgb24.png',
 | 
					 | 
				
			||||||
                 'rgb32bf.bmp': 'rgb24.png'
 | 
					 | 
				
			||||||
                 }     
 | 
					 | 
				
			||||||
                 
 | 
					 | 
				
			||||||
    def get_compare(f):
 | 
					 | 
				
			||||||
        (head, name) = os.path.split(f)
 | 
					 | 
				
			||||||
        if name in file_map:
 | 
					 | 
				
			||||||
            return os.path.join(base, 'html', file_map[name])
 | 
					 | 
				
			||||||
        (name,ext) = os.path.splitext(name)
 | 
					 | 
				
			||||||
        return os.path.join(base, 'html', "%s.png"%name)
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    for f in get_files('g'):
 | 
					 | 
				
			||||||
        try:
 | 
					 | 
				
			||||||
            im = Image.open(f)
 | 
					 | 
				
			||||||
            im.load()
 | 
					 | 
				
			||||||
            compare = Image.open(get_compare(f))
 | 
					 | 
				
			||||||
            compare.load()
 | 
					 | 
				
			||||||
            if im.mode == 'P':
 | 
					 | 
				
			||||||
                # assert image similar doesn't really work
 | 
					 | 
				
			||||||
                # with paletized image, since the palette might
 | 
					 | 
				
			||||||
                # be differently ordered for an equivalent image.
 | 
					 | 
				
			||||||
                im = im.convert('RGBA')
 | 
					 | 
				
			||||||
                compare = im.convert('RGBA')
 | 
					 | 
				
			||||||
            assert_image_similar(im, compare,5)
 | 
					 | 
				
			||||||
            
 | 
					 | 
				
			||||||
            
 | 
					 | 
				
			||||||
        except Exception as msg:
 | 
					 | 
				
			||||||
            # there are three here that are unsupported:
 | 
					 | 
				
			||||||
            unsupported = (os.path.join(base, 'g', 'rgb32bf.bmp'),
 | 
					 | 
				
			||||||
                           os.path.join(base, 'g', 'pal8rle.bmp'),
 | 
					 | 
				
			||||||
                           os.path.join(base, 'g', 'pal4rle.bmp'))
 | 
					 | 
				
			||||||
            if f not in unsupported:
 | 
					 | 
				
			||||||
                assert_true(False, "Unsupported Image %s: %s" %(f,msg))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,14 +0,0 @@
 | 
				
			||||||
from tester import *
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from PIL import Image
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# sample ppm stream
 | 
					 | 
				
			||||||
file = "Images/lena.psd"
 | 
					 | 
				
			||||||
data = open(file, "rb").read()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test_sanity():
 | 
					 | 
				
			||||||
    im = Image.open(file)
 | 
					 | 
				
			||||||
    im.load()
 | 
					 | 
				
			||||||
    assert_equal(im.mode, "RGB")
 | 
					 | 
				
			||||||
    assert_equal(im.size, (128, 128))
 | 
					 | 
				
			||||||
    assert_equal(im.format, "PSD")
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,64 +0,0 @@
 | 
				
			||||||
from tester import *
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from PIL import Image
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test_getcolors():
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def getcolors(mode, limit=None):
 | 
					 | 
				
			||||||
        im = lena(mode)
 | 
					 | 
				
			||||||
        if limit:
 | 
					 | 
				
			||||||
            colors = im.getcolors(limit)
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            colors = im.getcolors()
 | 
					 | 
				
			||||||
        if colors:
 | 
					 | 
				
			||||||
            return len(colors)
 | 
					 | 
				
			||||||
        return None
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    assert_equal(getcolors("1"), 2)
 | 
					 | 
				
			||||||
    assert_equal(getcolors("L"), 193)
 | 
					 | 
				
			||||||
    assert_equal(getcolors("I"), 193)
 | 
					 | 
				
			||||||
    assert_equal(getcolors("F"), 193)
 | 
					 | 
				
			||||||
    assert_equal(getcolors("P"), 54) # fixed palette
 | 
					 | 
				
			||||||
    assert_equal(getcolors("RGB"), None)
 | 
					 | 
				
			||||||
    assert_equal(getcolors("RGBA"), None)
 | 
					 | 
				
			||||||
    assert_equal(getcolors("CMYK"), None)
 | 
					 | 
				
			||||||
    assert_equal(getcolors("YCbCr"), None)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    assert_equal(getcolors("L", 128), None)
 | 
					 | 
				
			||||||
    assert_equal(getcolors("L", 1024), 193)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    assert_equal(getcolors("RGB", 8192), None)
 | 
					 | 
				
			||||||
    assert_equal(getcolors("RGB", 16384), 14836)
 | 
					 | 
				
			||||||
    assert_equal(getcolors("RGB", 100000), 14836)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    assert_equal(getcolors("RGBA", 16384), 14836)
 | 
					 | 
				
			||||||
    assert_equal(getcolors("CMYK", 16384), 14836)
 | 
					 | 
				
			||||||
    assert_equal(getcolors("YCbCr", 16384), 11995)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# --------------------------------------------------------------------
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test_pack():
 | 
					 | 
				
			||||||
    # Pack problems for small tables (@PIL209)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    im = lena().quantize(3).convert("RGB")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    expected = [(3236, (227, 183, 147)), (6297, (143, 84, 81)), (6851, (208, 143, 112))]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    A = im.getcolors(maxcolors=2)
 | 
					 | 
				
			||||||
    assert_equal(A, None)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    A = im.getcolors(maxcolors=3)
 | 
					 | 
				
			||||||
    A.sort()
 | 
					 | 
				
			||||||
    assert_equal(A, expected)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    A = im.getcolors(maxcolors=4)
 | 
					 | 
				
			||||||
    A.sort()
 | 
					 | 
				
			||||||
    assert_equal(A, expected)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    A = im.getcolors(maxcolors=8)
 | 
					 | 
				
			||||||
    A.sort()
 | 
					 | 
				
			||||||
    assert_equal(A, expected)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    A = im.getcolors(maxcolors=16)
 | 
					 | 
				
			||||||
    A.sort()
 | 
					 | 
				
			||||||
    assert_equal(A, expected)
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,15 +0,0 @@
 | 
				
			||||||
from tester import *
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from PIL import Image
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test_rotate():
 | 
					 | 
				
			||||||
    def rotate(mode):
 | 
					 | 
				
			||||||
        im = lena(mode)
 | 
					 | 
				
			||||||
        out = im.rotate(45)
 | 
					 | 
				
			||||||
        assert_equal(out.mode, mode)
 | 
					 | 
				
			||||||
        assert_equal(out.size, im.size) # default rotate clips output
 | 
					 | 
				
			||||||
        out = im.rotate(45, expand=1)
 | 
					 | 
				
			||||||
        assert_equal(out.mode, mode)
 | 
					 | 
				
			||||||
        assert_true(out.size != im.size)
 | 
					 | 
				
			||||||
    for mode in "1", "P", "L", "RGB", "I", "F":
 | 
					 | 
				
			||||||
        yield_test(rotate, mode)
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,36 +0,0 @@
 | 
				
			||||||
from tester import *
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from PIL import Image
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test_sanity():
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    im = lena()
 | 
					 | 
				
			||||||
    im.thumbnail((100, 100))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    assert_image(im, im.mode, (100, 100))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test_aspect():
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    im = lena()
 | 
					 | 
				
			||||||
    im.thumbnail((100, 100))
 | 
					 | 
				
			||||||
    assert_image(im, im.mode, (100, 100))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    im = lena().resize((128, 256))
 | 
					 | 
				
			||||||
    im.thumbnail((100, 100))
 | 
					 | 
				
			||||||
    assert_image(im, im.mode, (50, 100))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    im = lena().resize((128, 256))
 | 
					 | 
				
			||||||
    im.thumbnail((50, 100))
 | 
					 | 
				
			||||||
    assert_image(im, im.mode, (50, 100))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    im = lena().resize((256, 128))
 | 
					 | 
				
			||||||
    im.thumbnail((100, 100))
 | 
					 | 
				
			||||||
    assert_image(im, im.mode, (100, 50))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    im = lena().resize((256, 128))
 | 
					 | 
				
			||||||
    im.thumbnail((100, 50))
 | 
					 | 
				
			||||||
    assert_image(im, im.mode, (100, 50))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    im = lena().resize((128, 128))
 | 
					 | 
				
			||||||
    im.thumbnail((100, 100))
 | 
					 | 
				
			||||||
    assert_image(im, im.mode, (100, 100))
 | 
					 | 
				
			||||||
| 
						 | 
					@ -1,81 +0,0 @@
 | 
				
			||||||
from tester import *
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from PIL import Image
 | 
					 | 
				
			||||||
from PIL import ImageOps
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class Deformer:
 | 
					 | 
				
			||||||
    def getmesh(self, im):
 | 
					 | 
				
			||||||
        x, y = im.size
 | 
					 | 
				
			||||||
        return [((0, 0, x, y), (0, 0, x, 0, x, y, y, 0))]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
deformer = Deformer()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test_sanity():
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.autocontrast(lena("L"))
 | 
					 | 
				
			||||||
    ImageOps.autocontrast(lena("RGB"))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.autocontrast(lena("L"), cutoff=10)
 | 
					 | 
				
			||||||
    ImageOps.autocontrast(lena("L"), ignore=[0, 255])
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.colorize(lena("L"), (0, 0, 0), (255, 255, 255))
 | 
					 | 
				
			||||||
    ImageOps.colorize(lena("L"), "black", "white")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.crop(lena("L"), 1)
 | 
					 | 
				
			||||||
    ImageOps.crop(lena("RGB"), 1)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.deform(lena("L"), deformer)
 | 
					 | 
				
			||||||
    ImageOps.deform(lena("RGB"), deformer)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.equalize(lena("L"))
 | 
					 | 
				
			||||||
    ImageOps.equalize(lena("RGB"))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.expand(lena("L"), 1)
 | 
					 | 
				
			||||||
    ImageOps.expand(lena("RGB"), 1)
 | 
					 | 
				
			||||||
    ImageOps.expand(lena("L"), 2, "blue")
 | 
					 | 
				
			||||||
    ImageOps.expand(lena("RGB"), 2, "blue")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.fit(lena("L"), (128, 128))
 | 
					 | 
				
			||||||
    ImageOps.fit(lena("RGB"), (128, 128))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.flip(lena("L"))
 | 
					 | 
				
			||||||
    ImageOps.flip(lena("RGB"))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.grayscale(lena("L"))
 | 
					 | 
				
			||||||
    ImageOps.grayscale(lena("RGB"))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.invert(lena("L"))
 | 
					 | 
				
			||||||
    ImageOps.invert(lena("RGB"))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.mirror(lena("L"))
 | 
					 | 
				
			||||||
    ImageOps.mirror(lena("RGB"))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.posterize(lena("L"), 4)
 | 
					 | 
				
			||||||
    ImageOps.posterize(lena("RGB"), 4)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.solarize(lena("L"))
 | 
					 | 
				
			||||||
    ImageOps.solarize(lena("RGB"))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    success()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test_1pxfit():
 | 
					 | 
				
			||||||
    # Division by zero in equalize if image is 1 pixel high
 | 
					 | 
				
			||||||
    newimg = ImageOps.fit(lena("RGB").resize((1,1)), (35,35))
 | 
					 | 
				
			||||||
    assert_equal(newimg.size,(35,35))
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    newimg = ImageOps.fit(lena("RGB").resize((1,100)), (35,35))
 | 
					 | 
				
			||||||
    assert_equal(newimg.size,(35,35))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    newimg = ImageOps.fit(lena("RGB").resize((100,1)), (35,35))
 | 
					 | 
				
			||||||
    assert_equal(newimg.size,(35,35))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test_pil163():
 | 
					 | 
				
			||||||
    # Division by zero in equalize if < 255 pixels in image (@PIL163)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    i = lena("RGB").resize((15, 16))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ImageOps.equalize(i.convert("L"))
 | 
					 | 
				
			||||||
    ImageOps.equalize(i.convert("P"))
 | 
					 | 
				
			||||||
    ImageOps.equalize(i.convert("RGB"))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    success()
 | 
					 | 
				
			||||||
							
								
								
									
										5
									
								
								test/mini.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								test/mini.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,5 @@
 | 
				
			||||||
 | 
					from test_image_putpixel import TestImagePutPixel as put
 | 
				
			||||||
 | 
					from test_image_getpixel import TestImageGetPixel as get
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					dir(get)
 | 
				
			||||||
 | 
					get.test_basic()
 | 
				
			||||||
							
								
								
									
										94
									
								
								test/test_bmp_reference.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								test/test_bmp_reference.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,94 @@
 | 
				
			||||||
 | 
					from helper import unittest, PillowTestCase
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from PIL import Image
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					base = os.path.join('Tests', 'images', 'bmp')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestImage(PillowTestCase):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get_files(self, d, ext='.bmp'):
 | 
				
			||||||
 | 
					        return [os.path.join(base, d, f) for f
 | 
				
			||||||
 | 
					                in os.listdir(os.path.join(base, d)) if ext in f]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_bad(self):
 | 
				
			||||||
 | 
					        """ These shouldn't crash/dos, but they shouldn't return anything
 | 
				
			||||||
 | 
					        either """
 | 
				
			||||||
 | 
					        for f in self.get_files('b'):
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
 | 
					                im = Image.open(f)
 | 
				
			||||||
 | 
					                im.load()
 | 
				
			||||||
 | 
					            except Exception:  # as msg:
 | 
				
			||||||
 | 
					                pass
 | 
				
			||||||
 | 
					                # print ("Bad Image %s: %s" %(f,msg))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_questionable(self):
 | 
				
			||||||
 | 
					        """ These shouldn't crash/dos, but its not well defined that these
 | 
				
			||||||
 | 
					        are in spec """
 | 
				
			||||||
 | 
					        for f in self.get_files('q'):
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
 | 
					                im = Image.open(f)
 | 
				
			||||||
 | 
					                im.load()
 | 
				
			||||||
 | 
					            except Exception:  # as msg:
 | 
				
			||||||
 | 
					                pass
 | 
				
			||||||
 | 
					                # print ("Bad Image %s: %s" %(f,msg))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_good(self):
 | 
				
			||||||
 | 
					        """ These should all work. There's a set of target files in the
 | 
				
			||||||
 | 
					        html directory that we can compare against. """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Target files, if they're not just replacing the extension
 | 
				
			||||||
 | 
					        file_map = {'pal1wb.bmp': 'pal1.png',
 | 
				
			||||||
 | 
					                    'pal4rle.bmp': 'pal4.png',
 | 
				
			||||||
 | 
					                    'pal8-0.bmp': 'pal8.png',
 | 
				
			||||||
 | 
					                    'pal8rle.bmp': 'pal8.png',
 | 
				
			||||||
 | 
					                    'pal8topdown.bmp': 'pal8.png',
 | 
				
			||||||
 | 
					                    'pal8nonsquare.bmp': 'pal8nonsquare-v.png',
 | 
				
			||||||
 | 
					                    'pal8os2.bmp': 'pal8.png',
 | 
				
			||||||
 | 
					                    'pal8os2sp.bmp': 'pal8.png',
 | 
				
			||||||
 | 
					                    'pal8os2v2.bmp': 'pal8.png',
 | 
				
			||||||
 | 
					                    'pal8os2v2-16.bmp': 'pal8.png',
 | 
				
			||||||
 | 
					                    'pal8v4.bmp': 'pal8.png',
 | 
				
			||||||
 | 
					                    'pal8v5.bmp': 'pal8.png',
 | 
				
			||||||
 | 
					                    'rgb16-565pal.bmp': 'rgb16-565.png',
 | 
				
			||||||
 | 
					                    'rgb24pal.bmp': 'rgb24.png',
 | 
				
			||||||
 | 
					                    'rgb32.bmp': 'rgb24.png',
 | 
				
			||||||
 | 
					                    'rgb32bf.bmp': 'rgb24.png'
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        def get_compare(f):
 | 
				
			||||||
 | 
					            (head, name) = os.path.split(f)
 | 
				
			||||||
 | 
					            if name in file_map:
 | 
				
			||||||
 | 
					                return os.path.join(base, 'html', file_map[name])
 | 
				
			||||||
 | 
					            (name, ext) = os.path.splitext(name)
 | 
				
			||||||
 | 
					            return os.path.join(base, 'html', "%s.png" % name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for f in self.get_files('g'):
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
 | 
					                im = Image.open(f)
 | 
				
			||||||
 | 
					                im.load()
 | 
				
			||||||
 | 
					                compare = Image.open(get_compare(f))
 | 
				
			||||||
 | 
					                compare.load()
 | 
				
			||||||
 | 
					                if im.mode == 'P':
 | 
				
			||||||
 | 
					                    # assert image similar doesn't really work
 | 
				
			||||||
 | 
					                    # with paletized image, since the palette might
 | 
				
			||||||
 | 
					                    # be differently ordered for an equivalent image.
 | 
				
			||||||
 | 
					                    im = im.convert('RGBA')
 | 
				
			||||||
 | 
					                    compare = im.convert('RGBA')
 | 
				
			||||||
 | 
					                self.assert_image_similar(im, compare, 5)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            except Exception as msg:
 | 
				
			||||||
 | 
					                # there are three here that are unsupported:
 | 
				
			||||||
 | 
					                unsupported = (os.path.join(base, 'g', 'rgb32bf.bmp'),
 | 
				
			||||||
 | 
					                               os.path.join(base, 'g', 'pal8rle.bmp'),
 | 
				
			||||||
 | 
					                               os.path.join(base, 'g', 'pal4rle.bmp'))
 | 
				
			||||||
 | 
					                if f not in unsupported:
 | 
				
			||||||
 | 
					                    self.assertTrue(
 | 
				
			||||||
 | 
					                        False, "Unsupported Image %s: %s" % (f, msg))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 | 
					    unittest.main()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# End of file
 | 
				
			||||||
							
								
								
									
										23
									
								
								test/test_file_psd.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								test/test_file_psd.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,23 @@
 | 
				
			||||||
 | 
					from helper import unittest, PillowTestCase
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from PIL import Image
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# sample ppm stream
 | 
				
			||||||
 | 
					file = "Images/lena.psd"
 | 
				
			||||||
 | 
					data = open(file, "rb").read()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestImagePsd(PillowTestCase):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_sanity(self):
 | 
				
			||||||
 | 
					        im = Image.open(file)
 | 
				
			||||||
 | 
					        im.load()
 | 
				
			||||||
 | 
					        self.assertEqual(im.mode, "RGB")
 | 
				
			||||||
 | 
					        self.assertEqual(im.size, (128, 128))
 | 
				
			||||||
 | 
					        self.assertEqual(im.format, "PSD")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 | 
					    unittest.main()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# End of file
 | 
				
			||||||
							
								
								
									
										74
									
								
								test/test_image_getcolors.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								test/test_image_getcolors.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,74 @@
 | 
				
			||||||
 | 
					from helper import unittest, PillowTestCase, lena
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestImage(PillowTestCase):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_getcolors(self):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        def getcolors(mode, limit=None):
 | 
				
			||||||
 | 
					            im = lena(mode)
 | 
				
			||||||
 | 
					            if limit:
 | 
				
			||||||
 | 
					                colors = im.getcolors(limit)
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                colors = im.getcolors()
 | 
				
			||||||
 | 
					            if colors:
 | 
				
			||||||
 | 
					                return len(colors)
 | 
				
			||||||
 | 
					            return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("1"), 2)
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("L"), 193)
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("I"), 193)
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("F"), 193)
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("P"), 54)  # fixed palette
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("RGB"), None)
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("RGBA"), None)
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("CMYK"), None)
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("YCbCr"), None)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("L", 128), None)
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("L", 1024), 193)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("RGB", 8192), None)
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("RGB", 16384), 14836)
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("RGB", 100000), 14836)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("RGBA", 16384), 14836)
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("CMYK", 16384), 14836)
 | 
				
			||||||
 | 
					        self.assertEqual(getcolors("YCbCr", 16384), 11995)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # --------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_pack(self):
 | 
				
			||||||
 | 
					        # Pack problems for small tables (@PIL209)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        im = lena().quantize(3).convert("RGB")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        expected = [
 | 
				
			||||||
 | 
					            (3236, (227, 183, 147)),
 | 
				
			||||||
 | 
					            (6297, (143, 84, 81)),
 | 
				
			||||||
 | 
					            (6851, (208, 143, 112))]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        A = im.getcolors(maxcolors=2)
 | 
				
			||||||
 | 
					        self.assertEqual(A, None)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        A = im.getcolors(maxcolors=3)
 | 
				
			||||||
 | 
					        A.sort()
 | 
				
			||||||
 | 
					        self.assertEqual(A, expected)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        A = im.getcolors(maxcolors=4)
 | 
				
			||||||
 | 
					        A.sort()
 | 
				
			||||||
 | 
					        self.assertEqual(A, expected)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        A = im.getcolors(maxcolors=8)
 | 
				
			||||||
 | 
					        A.sort()
 | 
				
			||||||
 | 
					        self.assertEqual(A, expected)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        A = im.getcolors(maxcolors=16)
 | 
				
			||||||
 | 
					        A.sort()
 | 
				
			||||||
 | 
					        self.assertEqual(A, expected)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 | 
					    unittest.main()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# End of file
 | 
				
			||||||
							
								
								
									
										22
									
								
								test/test_image_rotate.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								test/test_image_rotate.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,22 @@
 | 
				
			||||||
 | 
					from helper import unittest, PillowTestCase, lena
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestImageRotate(PillowTestCase):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_rotate(self):
 | 
				
			||||||
 | 
					        def rotate(mode):
 | 
				
			||||||
 | 
					            im = lena(mode)
 | 
				
			||||||
 | 
					            out = im.rotate(45)
 | 
				
			||||||
 | 
					            self.assertEqual(out.mode, mode)
 | 
				
			||||||
 | 
					            self.assertEqual(out.size, im.size)  # default rotate clips output
 | 
				
			||||||
 | 
					            out = im.rotate(45, expand=1)
 | 
				
			||||||
 | 
					            self.assertEqual(out.mode, mode)
 | 
				
			||||||
 | 
					            self.assertNotEqual(out.size, im.size)
 | 
				
			||||||
 | 
					        for mode in "1", "P", "L", "RGB", "I", "F":
 | 
				
			||||||
 | 
					            rotate(mode)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 | 
					    unittest.main()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# End of file
 | 
				
			||||||
							
								
								
									
										43
									
								
								test/test_image_thumbnail.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								test/test_image_thumbnail.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,43 @@
 | 
				
			||||||
 | 
					from helper import unittest, PillowTestCase, lena
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestImageThumbnail(PillowTestCase):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_sanity(self):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        im = lena()
 | 
				
			||||||
 | 
					        im.thumbnail((100, 100))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assert_image(im, im.mode, (100, 100))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_aspect(self):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        im = lena()
 | 
				
			||||||
 | 
					        im.thumbnail((100, 100))
 | 
				
			||||||
 | 
					        self.assert_image(im, im.mode, (100, 100))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        im = lena().resize((128, 256))
 | 
				
			||||||
 | 
					        im.thumbnail((100, 100))
 | 
				
			||||||
 | 
					        self.assert_image(im, im.mode, (50, 100))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        im = lena().resize((128, 256))
 | 
				
			||||||
 | 
					        im.thumbnail((50, 100))
 | 
				
			||||||
 | 
					        self.assert_image(im, im.mode, (50, 100))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        im = lena().resize((256, 128))
 | 
				
			||||||
 | 
					        im.thumbnail((100, 100))
 | 
				
			||||||
 | 
					        self.assert_image(im, im.mode, (100, 50))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        im = lena().resize((256, 128))
 | 
				
			||||||
 | 
					        im.thumbnail((100, 50))
 | 
				
			||||||
 | 
					        self.assert_image(im, im.mode, (100, 50))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        im = lena().resize((128, 128))
 | 
				
			||||||
 | 
					        im.thumbnail((100, 100))
 | 
				
			||||||
 | 
					        self.assert_image(im, im.mode, (100, 100))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 | 
					    unittest.main()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# End of file
 | 
				
			||||||
							
								
								
									
										85
									
								
								test/test_imageops.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								test/test_imageops.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,85 @@
 | 
				
			||||||
 | 
					from helper import unittest, PillowTestCase, lena
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from PIL import ImageOps
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TestImageOps(PillowTestCase):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    class Deformer:
 | 
				
			||||||
 | 
					        def getmesh(self, im):
 | 
				
			||||||
 | 
					            x, y = im.size
 | 
				
			||||||
 | 
					            return [((0, 0, x, y), (0, 0, x, 0, x, y, y, 0))]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    deformer = Deformer()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_sanity(self):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.autocontrast(lena("L"))
 | 
				
			||||||
 | 
					        ImageOps.autocontrast(lena("RGB"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.autocontrast(lena("L"), cutoff=10)
 | 
				
			||||||
 | 
					        ImageOps.autocontrast(lena("L"), ignore=[0, 255])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.colorize(lena("L"), (0, 0, 0), (255, 255, 255))
 | 
				
			||||||
 | 
					        ImageOps.colorize(lena("L"), "black", "white")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.crop(lena("L"), 1)
 | 
				
			||||||
 | 
					        ImageOps.crop(lena("RGB"), 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.deform(lena("L"), self.deformer)
 | 
				
			||||||
 | 
					        ImageOps.deform(lena("RGB"), self.deformer)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.equalize(lena("L"))
 | 
				
			||||||
 | 
					        ImageOps.equalize(lena("RGB"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.expand(lena("L"), 1)
 | 
				
			||||||
 | 
					        ImageOps.expand(lena("RGB"), 1)
 | 
				
			||||||
 | 
					        ImageOps.expand(lena("L"), 2, "blue")
 | 
				
			||||||
 | 
					        ImageOps.expand(lena("RGB"), 2, "blue")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.fit(lena("L"), (128, 128))
 | 
				
			||||||
 | 
					        ImageOps.fit(lena("RGB"), (128, 128))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.flip(lena("L"))
 | 
				
			||||||
 | 
					        ImageOps.flip(lena("RGB"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.grayscale(lena("L"))
 | 
				
			||||||
 | 
					        ImageOps.grayscale(lena("RGB"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.invert(lena("L"))
 | 
				
			||||||
 | 
					        ImageOps.invert(lena("RGB"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.mirror(lena("L"))
 | 
				
			||||||
 | 
					        ImageOps.mirror(lena("RGB"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.posterize(lena("L"), 4)
 | 
				
			||||||
 | 
					        ImageOps.posterize(lena("RGB"), 4)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.solarize(lena("L"))
 | 
				
			||||||
 | 
					        ImageOps.solarize(lena("RGB"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_1pxfit(self):
 | 
				
			||||||
 | 
					        # Division by zero in equalize if image is 1 pixel high
 | 
				
			||||||
 | 
					        newimg = ImageOps.fit(lena("RGB").resize((1, 1)), (35, 35))
 | 
				
			||||||
 | 
					        self.assertEqual(newimg.size, (35, 35))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        newimg = ImageOps.fit(lena("RGB").resize((1, 100)), (35, 35))
 | 
				
			||||||
 | 
					        self.assertEqual(newimg.size, (35, 35))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        newimg = ImageOps.fit(lena("RGB").resize((100, 1)), (35, 35))
 | 
				
			||||||
 | 
					        self.assertEqual(newimg.size, (35, 35))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_pil163(self):
 | 
				
			||||||
 | 
					        # Division by zero in equalize if < 255 pixels in image (@PIL163)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        i = lena("RGB").resize((15, 16))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ImageOps.equalize(i.convert("L"))
 | 
				
			||||||
 | 
					        ImageOps.equalize(i.convert("P"))
 | 
				
			||||||
 | 
					        ImageOps.equalize(i.convert("RGB"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 | 
					    unittest.main()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# End of file
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user