mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 01:46:18 +03:00
improve tests
This commit is contained in:
parent
7faf18ccde
commit
cdd0624945
|
@ -238,3 +238,12 @@ if sys.platform == 'win32':
|
||||||
IMCONVERT = os.path.join(IMCONVERT, 'convert.exe')
|
IMCONVERT = os.path.join(IMCONVERT, 'convert.exe')
|
||||||
else:
|
else:
|
||||||
IMCONVERT = 'convert'
|
IMCONVERT = 'convert'
|
||||||
|
|
||||||
|
|
||||||
|
class cached_property(object):
|
||||||
|
def __init__(self, func):
|
||||||
|
self.func = func
|
||||||
|
|
||||||
|
def __get__(self, instance, cls=None):
|
||||||
|
result = instance.__dict__[self.func.__name__] = self.func(instance)
|
||||||
|
return result
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from helper import PillowTestCase
|
from helper import PillowTestCase, cached_property
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
|
@ -28,6 +28,7 @@ class TestImagingPaste(PillowTestCase):
|
||||||
]
|
]
|
||||||
self.assertEqual(actual, expected)
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
@cached_property
|
||||||
def mask_1(self):
|
def mask_1(self):
|
||||||
mask = Image.new('1', (self.size, self.size))
|
mask = Image.new('1', (self.size, self.size))
|
||||||
px = mask.load()
|
px = mask.load()
|
||||||
|
@ -36,9 +37,11 @@ class TestImagingPaste(PillowTestCase):
|
||||||
px[y, x] = (x + y) % 2
|
px[y, x] = (x + y) % 2
|
||||||
return mask
|
return mask
|
||||||
|
|
||||||
|
@cached_property
|
||||||
def mask_L(self):
|
def mask_L(self):
|
||||||
return self.gradient_L().transpose(Image.ROTATE_270)
|
return self.gradient_L.transpose(Image.ROTATE_270)
|
||||||
|
|
||||||
|
@cached_property
|
||||||
def gradient_L(self):
|
def gradient_L(self):
|
||||||
gradient = Image.new('L', (self.size, self.size))
|
gradient = Image.new('L', (self.size, self.size))
|
||||||
px = gradient.load()
|
px = gradient.load()
|
||||||
|
@ -47,33 +50,36 @@ class TestImagingPaste(PillowTestCase):
|
||||||
px[y, x] = (x + y) % 255
|
px[y, x] = (x + y) % 255
|
||||||
return gradient
|
return gradient
|
||||||
|
|
||||||
|
@cached_property
|
||||||
def gradient_RGB(self):
|
def gradient_RGB(self):
|
||||||
return Image.merge('RGB', [
|
return Image.merge('RGB', [
|
||||||
self.gradient_L(),
|
self.gradient_L,
|
||||||
self.gradient_L().transpose(Image.ROTATE_90),
|
self.gradient_L.transpose(Image.ROTATE_90),
|
||||||
self.gradient_L().transpose(Image.ROTATE_180),
|
self.gradient_L.transpose(Image.ROTATE_180),
|
||||||
])
|
])
|
||||||
|
|
||||||
|
@cached_property
|
||||||
def gradient_RGBA(self):
|
def gradient_RGBA(self):
|
||||||
return Image.merge('RGBA', [
|
return Image.merge('RGBA', [
|
||||||
self.gradient_L(),
|
self.gradient_L,
|
||||||
self.gradient_L().transpose(Image.ROTATE_90),
|
self.gradient_L.transpose(Image.ROTATE_90),
|
||||||
self.gradient_L().transpose(Image.ROTATE_180),
|
self.gradient_L.transpose(Image.ROTATE_180),
|
||||||
self.gradient_L().transpose(Image.ROTATE_270),
|
self.gradient_L.transpose(Image.ROTATE_270),
|
||||||
])
|
])
|
||||||
|
|
||||||
|
@cached_property
|
||||||
def gradient_RGBa(self):
|
def gradient_RGBa(self):
|
||||||
return Image.merge('RGBa', [
|
return Image.merge('RGBa', [
|
||||||
self.gradient_L(),
|
self.gradient_L,
|
||||||
self.gradient_L().transpose(Image.ROTATE_90),
|
self.gradient_L.transpose(Image.ROTATE_90),
|
||||||
self.gradient_L().transpose(Image.ROTATE_180),
|
self.gradient_L.transpose(Image.ROTATE_180),
|
||||||
self.gradient_L().transpose(Image.ROTATE_270),
|
self.gradient_L.transpose(Image.ROTATE_270),
|
||||||
])
|
])
|
||||||
|
|
||||||
def test_image_solid(self):
|
def test_image_solid(self):
|
||||||
for mode in ('RGBA', 'RGB', 'L'):
|
for mode in ('RGBA', 'RGB', 'L'):
|
||||||
im = Image.new(mode, (200, 200), 'red')
|
im = Image.new(mode, (200, 200), 'red')
|
||||||
im2 = getattr(self, 'gradient_' + mode)()
|
im2 = getattr(self, 'gradient_' + mode)
|
||||||
|
|
||||||
im.paste(im2, (12, 23))
|
im.paste(im2, (12, 23))
|
||||||
|
|
||||||
|
@ -83,10 +89,9 @@ class TestImagingPaste(PillowTestCase):
|
||||||
def test_image_mask_1(self):
|
def test_image_mask_1(self):
|
||||||
for mode in ('RGBA', 'RGB', 'L'):
|
for mode in ('RGBA', 'RGB', 'L'):
|
||||||
im = Image.new(mode, (200, 200), 'white')
|
im = Image.new(mode, (200, 200), 'white')
|
||||||
im2 = getattr(self, 'gradient_' + mode)()
|
im2 = getattr(self, 'gradient_' + mode)
|
||||||
mask = self.mask_1()
|
|
||||||
|
|
||||||
im.paste(im2, (0, 0), mask)
|
im.paste(im2, (0, 0), self.mask_1)
|
||||||
|
|
||||||
self.assert_9points_image(im, [
|
self.assert_9points_image(im, [
|
||||||
(255, 255, 255, 255),
|
(255, 255, 255, 255),
|
||||||
|
@ -103,11 +108,9 @@ class TestImagingPaste(PillowTestCase):
|
||||||
def test_image_mask_L(self):
|
def test_image_mask_L(self):
|
||||||
for mode in ('RGBA', 'RGB', 'L'):
|
for mode in ('RGBA', 'RGB', 'L'):
|
||||||
im = Image.new(mode, (200, 200), 'white')
|
im = Image.new(mode, (200, 200), 'white')
|
||||||
im2 = getattr(self, 'gradient_' + mode)()
|
im2 = getattr(self, 'gradient_' + mode)
|
||||||
mask = self.mask_L()
|
|
||||||
|
|
||||||
im.paste(im2, (0, 0), mask)
|
im.paste(im2, (0, 0), self.mask_L)
|
||||||
im.save('_test_image_mask_L {}.png'.format(mode))
|
|
||||||
|
|
||||||
self.assert_9points_image(im, [
|
self.assert_9points_image(im, [
|
||||||
(128, 191, 255, 191),
|
(128, 191, 255, 191),
|
||||||
|
@ -124,11 +127,9 @@ class TestImagingPaste(PillowTestCase):
|
||||||
def test_image_mask_RGBA(self):
|
def test_image_mask_RGBA(self):
|
||||||
for mode in ('RGBA', 'RGB', 'L'):
|
for mode in ('RGBA', 'RGB', 'L'):
|
||||||
im = Image.new(mode, (200, 200), 'white')
|
im = Image.new(mode, (200, 200), 'white')
|
||||||
im2 = getattr(self, 'gradient_' + mode)()
|
im2 = getattr(self, 'gradient_' + mode)
|
||||||
mask = self.gradient_RGBA()
|
|
||||||
|
|
||||||
im.paste(im2, (0, 0), mask)
|
im.paste(im2, (0, 0), self.gradient_RGBA)
|
||||||
im.save('_test_image_mask_RGBA {}.png'.format(mode))
|
|
||||||
|
|
||||||
self.assert_9points_image(im, [
|
self.assert_9points_image(im, [
|
||||||
(128, 191, 255, 191),
|
(128, 191, 255, 191),
|
||||||
|
@ -145,11 +146,9 @@ class TestImagingPaste(PillowTestCase):
|
||||||
def test_image_mask_RGBa(self):
|
def test_image_mask_RGBa(self):
|
||||||
for mode in ('RGBA', 'RGB', 'L'):
|
for mode in ('RGBA', 'RGB', 'L'):
|
||||||
im = Image.new(mode, (200, 200), 'white')
|
im = Image.new(mode, (200, 200), 'white')
|
||||||
im2 = getattr(self, 'gradient_' + mode)()
|
im2 = getattr(self, 'gradient_' + mode)
|
||||||
mask = self.gradient_RGBa()
|
|
||||||
|
|
||||||
im.paste(im2, (0, 0), mask)
|
im.paste(im2, (0, 0), self.gradient_RGBa)
|
||||||
im.save('_test_image_mask_RGBA {}.png'.format(mode))
|
|
||||||
|
|
||||||
self.assert_9points_image(im, [
|
self.assert_9points_image(im, [
|
||||||
(128, 255, 126, 255),
|
(128, 255, 126, 255),
|
||||||
|
@ -179,9 +178,9 @@ class TestImagingPaste(PillowTestCase):
|
||||||
def test_color_mask_1(self):
|
def test_color_mask_1(self):
|
||||||
for mode in ('RGBA', 'RGB', 'L'):
|
for mode in ('RGBA', 'RGB', 'L'):
|
||||||
im = Image.new(mode, (200, 200), (50, 60, 70, 80)[:len(mode)])
|
im = Image.new(mode, (200, 200), (50, 60, 70, 80)[:len(mode)])
|
||||||
mask = self.mask_1()
|
color = (10, 20, 30, 40)[:len(mode)]
|
||||||
|
|
||||||
im.paste((10, 20, 30, 40)[:len(mode)], (0, 0), mask)
|
im.paste(color, (0, 0), self.mask_1)
|
||||||
|
|
||||||
self.assert_9points_image(im, [
|
self.assert_9points_image(im, [
|
||||||
(50, 60, 70, 80),
|
(50, 60, 70, 80),
|
||||||
|
@ -197,11 +196,10 @@ class TestImagingPaste(PillowTestCase):
|
||||||
|
|
||||||
def test_color_mask_L(self):
|
def test_color_mask_L(self):
|
||||||
for mode in ('RGBA', 'RGB', 'L'):
|
for mode in ('RGBA', 'RGB', 'L'):
|
||||||
im = getattr(self, 'gradient_' + mode)()
|
im = getattr(self, 'gradient_' + mode).copy()
|
||||||
im2 = 'white'
|
color = 'white'
|
||||||
mask = self.mask_L()
|
|
||||||
|
|
||||||
im.paste(im2, (0, 0), mask)
|
im.paste(color, (0, 0), self.mask_L)
|
||||||
|
|
||||||
self.assert_9points_image(im, [
|
self.assert_9points_image(im, [
|
||||||
(127, 191, 254, 191),
|
(127, 191, 254, 191),
|
||||||
|
@ -217,11 +215,10 @@ class TestImagingPaste(PillowTestCase):
|
||||||
|
|
||||||
def test_color_mask_RGBA(self):
|
def test_color_mask_RGBA(self):
|
||||||
for mode in ('RGBA', 'RGB', 'L'):
|
for mode in ('RGBA', 'RGB', 'L'):
|
||||||
im = getattr(self, 'gradient_' + mode)()
|
im = getattr(self, 'gradient_' + mode).copy()
|
||||||
im2 = 'white'
|
color = 'white'
|
||||||
mask = self.gradient_RGBA()
|
|
||||||
|
|
||||||
im.paste(im2, (0, 0), mask)
|
im.paste(color, (0, 0), self.gradient_RGBA)
|
||||||
|
|
||||||
self.assert_9points_image(im, [
|
self.assert_9points_image(im, [
|
||||||
(127, 191, 254, 191),
|
(127, 191, 254, 191),
|
||||||
|
@ -237,11 +234,10 @@ class TestImagingPaste(PillowTestCase):
|
||||||
|
|
||||||
def test_color_mask_RGBa(self):
|
def test_color_mask_RGBa(self):
|
||||||
for mode in ('RGBA', 'RGB', 'L'):
|
for mode in ('RGBA', 'RGB', 'L'):
|
||||||
im = getattr(self, 'gradient_' + mode)()
|
im = getattr(self, 'gradient_' + mode).copy()
|
||||||
im2 = 'white'
|
color = 'white'
|
||||||
mask = self.gradient_RGBa()
|
|
||||||
|
|
||||||
im.paste(im2, (0, 0), mask)
|
im.paste(color, (0, 0), self.gradient_RGBa)
|
||||||
|
|
||||||
self.assert_9points_image(im, [
|
self.assert_9points_image(im, [
|
||||||
(255, 63, 126, 63),
|
(255, 63, 126, 63),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user