better paste tests

This commit is contained in:
homm 2016-09-19 02:49:41 +03:00
parent c90eecd8f3
commit 7faf18ccde

View File

@ -1,112 +1,256 @@
from helper import unittest, PillowTestCase, hopper
from helper import PillowTestCase
from PIL import Image
class TestImagingPaste(PillowTestCase):
masks = {}
size = 128
def mask_1(self, size):
mask = Image.new('1', size)
def assert_9points_image(self, im, expected):
expected = [
point[0]
if im.mode == 'L' else
point[:len(im.mode)]
for point in expected
]
px = im.load()
actual = [
px[0, 0],
px[self.size // 2, 0],
px[self.size - 1, 0],
px[0, self.size // 2],
px[self.size // 2, self.size // 2],
px[self.size - 1, self.size // 2],
px[0, self.size - 1],
px[self.size // 2, self.size - 1],
px[self.size - 1, self.size - 1],
]
self.assertEqual(actual, expected)
def mask_1(self):
mask = Image.new('1', (self.size, self.size))
px = mask.load()
for y in range(mask.height):
for x in range(mask.width):
px[y, x] = (x + y) % 2
return mask
def mask_L(self, size):
mask = Image.new('L', size)
px = mask.load()
for y in range(mask.height):
for x in range(mask.width):
def mask_L(self):
return self.gradient_L().transpose(Image.ROTATE_270)
def gradient_L(self):
gradient = Image.new('L', (self.size, self.size))
px = gradient.load()
for y in range(gradient.height):
for x in range(gradient.width):
px[y, x] = (x + y) % 255
return mask
return gradient
def mask_RGBA(self, size):
mask = Image.new('RGB', size, 'red').split()
return Image.merge('RGBA', mask + (self.mask_L(size),))
def gradient_RGB(self):
return Image.merge('RGB', [
self.gradient_L(),
self.gradient_L().transpose(Image.ROTATE_90),
self.gradient_L().transpose(Image.ROTATE_180),
])
def mask_RGBa(self, size):
mask = Image.new('RGB', size, 'red').split()
return Image.merge('RGBa', mask + (self.mask_L(size),))
def gradient_RGBA(self):
return Image.merge('RGBA', [
self.gradient_L(),
self.gradient_L().transpose(Image.ROTATE_90),
self.gradient_L().transpose(Image.ROTATE_180),
self.gradient_L().transpose(Image.ROTATE_270),
])
def gradient_RGBa(self):
return Image.merge('RGBa', [
self.gradient_L(),
self.gradient_L().transpose(Image.ROTATE_90),
self.gradient_L().transpose(Image.ROTATE_180),
self.gradient_L().transpose(Image.ROTATE_270),
])
def test_image_solid(self):
for mode in ('L', 'RGB'):
for mode in ('RGBA', 'RGB', 'L'):
im = Image.new(mode, (200, 200), 'red')
im2 = hopper(mode)
im2 = getattr(self, 'gradient_' + mode)()
im.paste(im2, (0, 0))
im.paste(im2, (12, 23))
im = im.crop((0, 0) + im2.size)
im = im.crop((12, 23, im2.width + 12, im2.height + 23))
self.assert_image_equal(im, im2)
def test_image_mask_1(self):
for mode in ('L', 'RGB'):
im = Image.new(mode, (200, 200), 'red')
im2 = hopper(mode)
mask = self.mask_1(im2.size)
for mode in ('RGBA', 'RGB', 'L'):
im = Image.new(mode, (200, 200), 'white')
im2 = getattr(self, 'gradient_' + mode)()
mask = self.mask_1()
im.paste(im2, (0, 0), mask)
self.assert_9points_image(im, [
(255, 255, 255, 255),
(255, 255, 255, 255),
(127, 254, 127, 0),
(255, 255, 255, 255),
(255, 255, 255, 255),
(191, 190, 63, 64),
(127, 0, 127, 254),
(191, 64, 63, 190),
(255, 255, 255, 255),
])
def test_image_mask_L(self):
for mode in ('L', 'RGB'):
im = Image.new(mode, (200, 200), 'red')
im2 = hopper(mode)
mask = self.mask_L(im2.size)
for mode in ('RGBA', 'RGB', 'L'):
im = Image.new(mode, (200, 200), 'white')
im2 = getattr(self, 'gradient_' + mode)()
mask = self.mask_L()
im.paste(im2, (0, 0), mask)
im.save('_test_image_mask_L {}.png'.format(mode))
self.assert_9points_image(im, [
(128, 191, 255, 191),
(208, 239, 239, 208),
(255, 255, 255, 255),
(112, 111, 206, 207),
(192, 191, 191, 191),
(239, 239, 207, 207),
(128, 1, 128, 254),
(207, 113, 112, 207),
(255, 191, 128, 191),
])
def test_image_mask_RGBA(self):
for mode in ('L', 'RGB'):
im = Image.new(mode, (200, 200), 'red')
im2 = hopper(mode)
mask = self.mask_RGBA(im2.size)
for mode in ('RGBA', 'RGB', 'L'):
im = Image.new(mode, (200, 200), 'white')
im2 = getattr(self, 'gradient_' + mode)()
mask = self.gradient_RGBA()
im.paste(im2, (0, 0), mask)
im.save('_test_image_mask_RGBA {}.png'.format(mode))
self.assert_9points_image(im, [
(128, 191, 255, 191),
(208, 239, 239, 208),
(255, 255, 255, 255),
(112, 111, 206, 207),
(192, 191, 191, 191),
(239, 239, 207, 207),
(128, 1, 128, 254),
(207, 113, 112, 207),
(255, 191, 128, 191),
])
def test_image_mask_RGBa(self):
for mode in ('L', 'RGB'):
im = Image.new(mode, (200, 200), 'red')
im2 = hopper(mode)
mask = self.mask_RGBa(im2.size)
for mode in ('RGBA', 'RGB', 'L'):
im = Image.new(mode, (200, 200), 'white')
im2 = getattr(self, 'gradient_' + mode)()
mask = self.gradient_RGBa()
im.paste(im2, (0, 0), mask)
im.save('_test_image_mask_RGBA {}.png'.format(mode))
self.assert_9points_image(im, [
(128, 255, 126, 255),
(0, 127, 126, 255),
(126, 253, 126, 255),
(128, 127, 254, 255),
(0, 255, 254, 255),
(126, 125, 254, 255),
(128, 1, 128, 255),
(0, 129, 128, 255),
(126, 255, 128, 255),
])
def test_color_solid(self):
for mode in ('L', 'RGB'):
im = hopper(mode)
im2 = 'red'
for mode in ('RGBA', 'RGB', 'L'):
im = Image.new(mode, (200, 200), 'black')
im.paste(im2)
rect = (12, 23, 128 + 12, 128 + 23)
im.paste('white', rect)
hist = im.crop(rect).histogram()
while hist:
head, hist = hist[:256], hist[256:]
self.assertEqual(head[255], 128 * 128)
self.assertEqual(sum(head[:255]), 0)
def test_color_mask_1(self):
for mode in ('L', 'RGB'):
im = hopper(mode)
im2 = 'red'
mask = self.mask_1(im.size)
for mode in ('RGBA', 'RGB', 'L'):
im = Image.new(mode, (200, 200), (50, 60, 70, 80)[:len(mode)])
mask = self.mask_1()
im.paste(im2, (0, 0), mask)
im.paste((10, 20, 30, 40)[:len(mode)], (0, 0), mask)
self.assert_9points_image(im, [
(50, 60, 70, 80),
(50, 60, 70, 80),
(10, 20, 30, 40),
(50, 60, 70, 80),
(50, 60, 70, 80),
(10, 20, 30, 40),
(10, 20, 30, 40),
(10, 20, 30, 40),
(50, 60, 70, 80),
])
def test_color_mask_L(self):
for mode in ('L', 'RGB'):
im = hopper(mode)
im2 = 'red'
mask = self.mask_L(im.size)
for mode in ('RGBA', 'RGB', 'L'):
im = getattr(self, 'gradient_' + mode)()
im2 = 'white'
mask = self.mask_L()
im.paste(im2, (0, 0), mask)
self.assert_9points_image(im, [
(127, 191, 254, 191),
(111, 207, 206, 110),
(127, 254, 127, 0),
(207, 207, 239, 239),
(191, 191, 190, 191),
(207, 206, 111, 112),
(254, 254, 254, 255),
(239, 206, 206, 238),
(254, 191, 127, 191),
])
def test_color_mask_RGBA(self):
for mode in ('L', 'RGB'):
im = hopper(mode)
im2 = 'red'
mask = self.mask_RGBA(im.size)
for mode in ('RGBA', 'RGB', 'L'):
im = getattr(self, 'gradient_' + mode)()
im2 = 'white'
mask = self.gradient_RGBA()
im.paste(im2, (0, 0), mask)
self.assert_9points_image(im, [
(127, 191, 254, 191),
(111, 207, 206, 110),
(127, 254, 127, 0),
(207, 207, 239, 239),
(191, 191, 190, 191),
(207, 206, 111, 112),
(254, 254, 254, 255),
(239, 206, 206, 238),
(254, 191, 127, 191),
])
def test_color_mask_RGBa(self):
for mode in ('L', 'RGB'):
im = hopper(mode)
im2 = 'red'
mask = self.mask_RGBa(im.size)
for mode in ('RGBA', 'RGB', 'L'):
im = getattr(self, 'gradient_' + mode)()
im2 = 'white'
mask = self.gradient_RGBa()
im.paste(im2, (0, 0), mask)
self.assert_9points_image(im, [
(255, 63, 126, 63),
(47, 143, 142, 46),
(126, 253, 126, 255),
(15, 15, 47, 47),
(63, 63, 62, 63),
(142, 141, 46, 47),
(255, 255, 255, 0),
(48, 15, 15, 47),
(126, 63, 255, 63)
])