Skip to content

Commit

Permalink
Merge a10597c into fdbd719
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jun 24, 2018
2 parents fdbd719 + a10597c commit ec76006
Show file tree
Hide file tree
Showing 61 changed files with 1,167 additions and 686 deletions.
6 changes: 4 additions & 2 deletions Tests/helper.py
Expand Up @@ -153,7 +153,8 @@ def assert_image_similar(self, a, b, epsilon, msg=None):
pass
raise e

def assert_image_similar_tofile(self, a, filename, epsilon, msg=None, mode=None):
def assert_image_similar_tofile(self, a, filename, epsilon, msg=None,
mode=None):
with Image.open(filename) as img:
if mode:
img = img.convert(mode)
Expand Down Expand Up @@ -246,7 +247,8 @@ def _get_mem_usage(self):
mem = getrusage(RUSAGE_SELF).ru_maxrss
if sys.platform == 'darwin':
# man 2 getrusage:
# ru_maxrss the maximum resident set size utilized (in bytes).
# ru_maxrss
# This is the maximum resident set size utilized (in bytes).
return mem / 1024 # Kb
else:
# linux
Expand Down
159 changes: 86 additions & 73 deletions Tests/test_color_lut.py
Expand Up @@ -22,8 +22,8 @@ def generate_identity_table(self, channels, size):
g / float(size2D-1) if size2D != 1 else 0,
][:channels]
for b in range(size3D)
for g in range(size2D)
for r in range(size1D)
for g in range(size2D)
for r in range(size1D)
]
return (
channels, size1D, size2D, size3D,
Expand All @@ -33,112 +33,123 @@ def test_wrong_args(self):
im = Image.new('RGB', (10, 10), 0)

with self.assertRaisesRegex(ValueError, "filter"):
im.im.color_lut_3d('RGB', Image.CUBIC,
*self.generate_identity_table(3, 3))
im.im.color_lut_3d('RGB',
Image.CUBIC,
*self.generate_identity_table(3, 3))

with self.assertRaisesRegex(ValueError, "image mode"):
im.im.color_lut_3d('wrong', Image.LINEAR,
*self.generate_identity_table(3, 3))
im.im.color_lut_3d('wrong',
Image.LINEAR,
*self.generate_identity_table(3, 3))

with self.assertRaisesRegex(ValueError, "table_channels"):
im.im.color_lut_3d('RGB', Image.LINEAR,
*self.generate_identity_table(5, 3))
im.im.color_lut_3d('RGB',
Image.LINEAR,
*self.generate_identity_table(5, 3))

with self.assertRaisesRegex(ValueError, "table_channels"):
im.im.color_lut_3d('RGB', Image.LINEAR,
*self.generate_identity_table(1, 3))
im.im.color_lut_3d('RGB',
Image.LINEAR,
*self.generate_identity_table(1, 3))

with self.assertRaisesRegex(ValueError, "table_channels"):
im.im.color_lut_3d('RGB', Image.LINEAR,
*self.generate_identity_table(2, 3))
im.im.color_lut_3d('RGB',
Image.LINEAR,
*self.generate_identity_table(2, 3))

with self.assertRaisesRegex(ValueError, "Table size"):
im.im.color_lut_3d('RGB', Image.LINEAR,
*self.generate_identity_table(3, (1, 3, 3)))
im.im.color_lut_3d('RGB',
Image.LINEAR,
*self.generate_identity_table(3, (1, 3, 3)))

with self.assertRaisesRegex(ValueError, "Table size"):
im.im.color_lut_3d('RGB', Image.LINEAR,
*self.generate_identity_table(3, (66, 3, 3)))
im.im.color_lut_3d('RGB',
Image.LINEAR,
*self.generate_identity_table(3, (66, 3, 3)))

with self.assertRaisesRegex(ValueError, r"size1D \* size2D \* size3D"):
im.im.color_lut_3d('RGB', Image.LINEAR,
3, 2, 2, 2, [0, 0, 0] * 7)
im.im.color_lut_3d('RGB',
Image.LINEAR,
3, 2, 2, 2, [0, 0, 0] * 7)

with self.assertRaisesRegex(ValueError, r"size1D \* size2D \* size3D"):
im.im.color_lut_3d('RGB', Image.LINEAR,
3, 2, 2, 2, [0, 0, 0] * 9)
im.im.color_lut_3d('RGB',
Image.LINEAR,
3, 2, 2, 2, [0, 0, 0] * 9)

with self.assertRaises(TypeError):
im.im.color_lut_3d('RGB', Image.LINEAR,
3, 2, 2, 2, [0, 0, "0"] * 8)
im.im.color_lut_3d('RGB',
Image.LINEAR,
3, 2, 2, 2, [0, 0, "0"] * 8)

with self.assertRaises(TypeError):
im.im.color_lut_3d('RGB', Image.LINEAR,
3, 2, 2, 2, 16)
im.im.color_lut_3d('RGB',
Image.LINEAR,
3, 2, 2, 2, 16)

def test_correct_args(self):
im = Image.new('RGB', (10, 10), 0)

im.im.color_lut_3d('RGB', Image.LINEAR,
*self.generate_identity_table(3, 3))
*self.generate_identity_table(3, 3))

im.im.color_lut_3d('CMYK', Image.LINEAR,
*self.generate_identity_table(4, 3))
*self.generate_identity_table(4, 3))

im.im.color_lut_3d('RGB', Image.LINEAR,
*self.generate_identity_table(3, (2, 3, 3)))
*self.generate_identity_table(3, (2, 3, 3)))

im.im.color_lut_3d('RGB', Image.LINEAR,
*self.generate_identity_table(3, (65, 3, 3)))
*self.generate_identity_table(3, (65, 3, 3)))

im.im.color_lut_3d('RGB', Image.LINEAR,
*self.generate_identity_table(3, (3, 65, 3)))
*self.generate_identity_table(3, (3, 65, 3)))

im.im.color_lut_3d('RGB', Image.LINEAR,
*self.generate_identity_table(3, (3, 3, 65)))
*self.generate_identity_table(3, (3, 3, 65)))

def test_wrong_mode(self):
with self.assertRaisesRegex(ValueError, "wrong mode"):
im = Image.new('L', (10, 10), 0)
im.im.color_lut_3d('RGB', Image.LINEAR,
*self.generate_identity_table(3, 3))
*self.generate_identity_table(3, 3))

with self.assertRaisesRegex(ValueError, "wrong mode"):
im = Image.new('RGB', (10, 10), 0)
im.im.color_lut_3d('L', Image.LINEAR,
*self.generate_identity_table(3, 3))
*self.generate_identity_table(3, 3))

with self.assertRaisesRegex(ValueError, "wrong mode"):
im = Image.new('L', (10, 10), 0)
im.im.color_lut_3d('L', Image.LINEAR,
*self.generate_identity_table(3, 3))
*self.generate_identity_table(3, 3))

with self.assertRaisesRegex(ValueError, "wrong mode"):
im = Image.new('RGB', (10, 10), 0)
im.im.color_lut_3d('RGBA', Image.LINEAR,
*self.generate_identity_table(3, 3))
*self.generate_identity_table(3, 3))

with self.assertRaisesRegex(ValueError, "wrong mode"):
im = Image.new('RGB', (10, 10), 0)
im.im.color_lut_3d('RGB', Image.LINEAR,
*self.generate_identity_table(4, 3))
*self.generate_identity_table(4, 3))

def test_correct_mode(self):
im = Image.new('RGBA', (10, 10), 0)
im.im.color_lut_3d('RGBA', Image.LINEAR,
*self.generate_identity_table(3, 3))
*self.generate_identity_table(3, 3))

im = Image.new('RGBA', (10, 10), 0)
im.im.color_lut_3d('RGBA', Image.LINEAR,
*self.generate_identity_table(4, 3))
*self.generate_identity_table(4, 3))

im = Image.new('RGB', (10, 10), 0)
im.im.color_lut_3d('HSV', Image.LINEAR,
*self.generate_identity_table(3, 3))
*self.generate_identity_table(3, 3))

im = Image.new('RGB', (10, 10), 0)
im.im.color_lut_3d('RGBA', Image.LINEAR,
*self.generate_identity_table(4, 3))
*self.generate_identity_table(4, 3))

def test_identities(self):
g = Image.linear_gradient('L')
Expand All @@ -149,12 +160,12 @@ def test_identities(self):
for size in [2, 3, 5, 7, 11, 16, 17]:
self.assert_image_equal(im, im._new(
im.im.color_lut_3d('RGB', Image.LINEAR,
*self.generate_identity_table(3, size))))
*self.generate_identity_table(3, size))))

# Not so fast
self.assert_image_equal(im, im._new(
im.im.color_lut_3d('RGB', Image.LINEAR,
*self.generate_identity_table(3, (2, 2, 65)))))
*self.generate_identity_table(3, (2, 2, 65)))))

def test_identities_4_channels(self):
g = Image.linear_gradient('L')
Expand All @@ -165,7 +176,7 @@ def test_identities_4_channels(self):
self.assert_image_equal(
Image.merge('RGBA', (im.split()*2)[:4]),
im._new(im.im.color_lut_3d('RGBA', Image.LINEAR,
*self.generate_identity_table(4, 17))))
*self.generate_identity_table(4, 17))))

def test_copy_alpha_channel(self):
g = Image.linear_gradient('L')
Expand All @@ -175,7 +186,7 @@ def test_copy_alpha_channel(self):

self.assert_image_equal(im, im._new(
im.im.color_lut_3d('RGBA', Image.LINEAR,
*self.generate_identity_table(3, 17))))
*self.generate_identity_table(3, 17))))

def test_channels_order(self):
g = Image.linear_gradient('L')
Expand All @@ -186,28 +197,28 @@ def test_channels_order(self):
self.assert_image_equal(
Image.merge('RGB', im.split()[::-1]),
im._new(im.im.color_lut_3d('RGB', Image.LINEAR,
3, 2, 2, 2, [
0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 1, 1,
3, 2, 2, 2, [
0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 1, 1,

1, 0, 0, 1, 0, 1,
1, 1, 0, 1, 1, 1,
])))
1, 0, 0, 1, 0, 1,
1, 1, 0, 1, 1, 1,
])))

def test_overflow(self):
g = Image.linear_gradient('L')
im = Image.merge('RGB', [g, g.transpose(Image.ROTATE_90),
g.transpose(Image.ROTATE_180)])

transformed = im._new(im.im.color_lut_3d('RGB', Image.LINEAR,
3, 2, 2, 2,
[
-1, -1, -1, 2, -1, -1,
-1, 2, -1, 2, 2, -1,

-1, -1, 2, 2, -1, 2,
-1, 2, 2, 2, 2, 2,
])).load()
3, 2, 2, 2,
[
-1, -1, -1, 2, -1, -1,
-1, 2, -1, 2, 2, -1,

-1, -1, 2, 2, -1, 2,
-1, 2, 2, 2, 2, 2,
])).load()
self.assertEqual(transformed[0, 0], (0, 0, 255))
self.assertEqual(transformed[50, 50], (0, 0, 255))
self.assertEqual(transformed[255, 0], (0, 255, 255))
Expand All @@ -218,14 +229,14 @@ def test_overflow(self):
self.assertEqual(transformed[205, 205], (255, 255, 0))

transformed = im._new(im.im.color_lut_3d('RGB', Image.LINEAR,
3, 2, 2, 2,
[
-3, -3, -3, 5, -3, -3,
-3, 5, -3, 5, 5, -3,

-3, -3, 5, 5, -3, 5,
-3, 5, 5, 5, 5, 5,
])).load()
3, 2, 2, 2,
[
-3, -3, -3, 5, -3, -3,
-3, 5, -3, 5, 5, -3,

-3, -3, 5, 5, -3, 5,
-3, 5, 5, 5, 5, 5,
])).load()
self.assertEqual(transformed[0, 0], (0, 0, 255))
self.assertEqual(transformed[50, 50], (0, 0, 255))
self.assertEqual(transformed[255, 0], (0, 255, 255))
Expand Down Expand Up @@ -287,22 +298,23 @@ def test_repr(self):
lut = ImageFilter.Color3DLUT(
(3, 4, 5), array('f', [0, 0, 0, 0] * (3 * 4 * 5)),
channels=4, target_mode='YCbCr', _copy_table=False)
self.assertEqual(repr(lut),
self.assertEqual(
repr(lut),
"<Color3DLUT from array size=3x4x5 channels=4 target_mode=YCbCr>")


class TestGenerateColorLut3D(PillowTestCase):
def test_wrong_channels_count(self):
with self.assertRaisesRegex(ValueError, "3 or 4 output channels"):
ImageFilter.Color3DLUT.generate(5, channels=2,
callback=lambda r, g, b: (r, g, b))
ImageFilter.Color3DLUT.generate(
5, channels=2, callback=lambda r, g, b: (r, g, b))

with self.assertRaisesRegex(ValueError, "should have either channels"):
ImageFilter.Color3DLUT.generate(5, lambda r, g, b: (r, g, b, r))

with self.assertRaisesRegex(ValueError, "should have either channels"):
ImageFilter.Color3DLUT.generate(5, channels=4,
callback=lambda r, g, b: (r, g, b))
ImageFilter.Color3DLUT.generate(
5, channels=4, callback=lambda r, g, b: (r, g, b))

def test_3_channels(self):
lut = ImageFilter.Color3DLUT.generate(5, lambda r, g, b: (r, g, b))
Expand All @@ -313,13 +325,14 @@ def test_3_channels(self):
1.0, 0.0, 0.0, 0.0, 0.25, 0.0, 0.25, 0.25, 0.0, 0.5, 0.25, 0.0])

def test_4_channels(self):
lut = ImageFilter.Color3DLUT.generate(5, channels=4,
callback=lambda r, g, b: (b, r, g, (r+g+b) / 2))
lut = ImageFilter.Color3DLUT.generate(
5, channels=4, callback=lambda r, g, b: (b, r, g, (r+g+b) / 2))
self.assertEqual(tuple(lut.size), (5, 5, 5))
self.assertEqual(lut.name, "Color 3D LUT")
self.assertEqual(lut.table[:24], [
0.0, 0.0, 0.0, 0.0, 0.0, 0.25, 0.0, 0.125, 0.0, 0.5, 0.0, 0.25,
0.0, 0.75, 0.0, 0.375, 0.0, 1.0, 0.0, 0.5, 0.0, 0.0, 0.25, 0.125])
0.0, 0.75, 0.0, 0.375, 0.0, 1.0, 0.0, 0.5, 0.0, 0.0, 0.25, 0.125
])

def test_apply(self):
lut = ImageFilter.Color3DLUT.generate(5, lambda r, g, b: (r, g, b))
Expand Down
3 changes: 2 additions & 1 deletion Tests/test_file_gif.py
Expand Up @@ -402,7 +402,8 @@ def test_background(self):

def test_comment(self):
im = Image.open(TEST_GIF)
self.assertEqual(im.info['comment'], b"File written by Adobe Photoshop\xa8 4.0")
self.assertEqual(im.info['comment'],
b"File written by Adobe Photoshop\xa8 4.0")

out = self.tempfile('temp.gif')
im = Image.new('L', (100, 100), '#000')
Expand Down
1 change: 0 additions & 1 deletion Tests/test_file_jpeg.py
Expand Up @@ -363,7 +363,6 @@ def test_truncated_jpeg_throws_IOError(self):
with self.assertRaises(IOError):
im.load()


def _n_qtables_helper(self, n, test_file):
im = Image.open(test_file)
f = self.tempfile('temp.jpg')
Expand Down

0 comments on commit ec76006

Please sign in to comment.