|
5 | 5 | from PIL import Image, ImageFilter |
6 | 6 | from helper import unittest, PillowTestCase |
7 | 7 |
|
| 8 | +try: |
| 9 | + import numpy |
| 10 | +except ImportError: |
| 11 | + numpy = None |
| 12 | + |
8 | 13 |
|
9 | 14 | class TestColorLut3DCoreAPI(PillowTestCase): |
10 | 15 | def generate_identity_table(self, channels, size): |
@@ -279,6 +284,80 @@ def test_convert_table(self): |
279 | 284 | lut = ImageFilter.Color3DLUT((2, 2, 2), [(0, 1, 2, 3)] * 8, |
280 | 285 | channels=4) |
281 | 286 |
|
| 287 | + @unittest.skipIf(numpy is None, "Numpy is not installed") |
| 288 | + def test_numpy_sources(self): |
| 289 | + table = numpy.ones((5, 6, 7, 3), dtype=numpy.float16) |
| 290 | + with self.assertRaisesRegex(ValueError, "should have either channels"): |
| 291 | + lut = ImageFilter.Color3DLUT((5, 6, 7), table) |
| 292 | + |
| 293 | + table = numpy.ones((7, 6, 5, 3), dtype=numpy.float16) |
| 294 | + lut = ImageFilter.Color3DLUT((5, 6, 7), table) |
| 295 | + self.assertIsInstance(lut.table, numpy.ndarray) |
| 296 | + self.assertEqual(lut.table.dtype, table.dtype) |
| 297 | + self.assertEqual(lut.table.shape, (table.size,)) |
| 298 | + |
| 299 | + table = numpy.ones((7 * 6 * 5, 3), dtype=numpy.float16) |
| 300 | + lut = ImageFilter.Color3DLUT((5, 6, 7), table) |
| 301 | + self.assertEqual(lut.table.shape, (table.size,)) |
| 302 | + |
| 303 | + table = numpy.ones((7 * 6 * 5 * 3), dtype=numpy.float16) |
| 304 | + lut = ImageFilter.Color3DLUT((5, 6, 7), table) |
| 305 | + self.assertEqual(lut.table.shape, (table.size,)) |
| 306 | + |
| 307 | + # Check application |
| 308 | + Image.new('RGB', (10, 10), 0).filter(lut) |
| 309 | + |
| 310 | + # Check copy |
| 311 | + table[0] = 33 |
| 312 | + self.assertEqual(lut.table[0], 1) |
| 313 | + |
| 314 | + # Check not copy |
| 315 | + table = numpy.ones((7 * 6 * 5 * 3), dtype=numpy.float16) |
| 316 | + lut = ImageFilter.Color3DLUT((5, 6, 7), table, _copy_table=False) |
| 317 | + table[0] = 33 |
| 318 | + self.assertEqual(lut.table[0], 33) |
| 319 | + |
| 320 | + @unittest.skipIf(numpy is None, "Numpy is not installed") |
| 321 | + def test_numpy_formats(self): |
| 322 | + g = Image.linear_gradient('L') |
| 323 | + im = Image.merge('RGB', [g, g.transpose(Image.ROTATE_90), |
| 324 | + g.transpose(Image.ROTATE_180)]) |
| 325 | + |
| 326 | + lut = ImageFilter.Color3DLUT.generate((7, 9, 11), |
| 327 | + lambda r, g, b: (r, g, b)) |
| 328 | + lut.table = numpy.array(lut.table, dtype=numpy.float32)[:-1] |
| 329 | + with self.assertRaisesRegex(ValueError, "should have table_channels"): |
| 330 | + im.filter(lut) |
| 331 | + |
| 332 | + lut = ImageFilter.Color3DLUT.generate((7, 9, 11), |
| 333 | + lambda r, g, b: (r, g, b)) |
| 334 | + lut.table = (numpy.array(lut.table, dtype=numpy.float32) |
| 335 | + .reshape((7 * 9 * 11), 3)) |
| 336 | + with self.assertRaisesRegex(ValueError, "should have table_channels"): |
| 337 | + im.filter(lut) |
| 338 | + |
| 339 | + lut = ImageFilter.Color3DLUT.generate((7, 9, 11), |
| 340 | + lambda r, g, b: (r, g, b)) |
| 341 | + lut.table = numpy.array(lut.table, dtype=numpy.float16) |
| 342 | + self.assert_image_equal(im, im.filter(lut)) |
| 343 | + |
| 344 | + lut = ImageFilter.Color3DLUT.generate((7, 9, 11), |
| 345 | + lambda r, g, b: (r, g, b)) |
| 346 | + lut.table = numpy.array(lut.table, dtype=numpy.float32) |
| 347 | + self.assert_image_equal(im, im.filter(lut)) |
| 348 | + |
| 349 | + lut = ImageFilter.Color3DLUT.generate((7, 9, 11), |
| 350 | + lambda r, g, b: (r, g, b)) |
| 351 | + lut.table = numpy.array(lut.table, dtype=numpy.float64) |
| 352 | + self.assert_image_equal(im, im.filter(lut)) |
| 353 | + |
| 354 | + lut = ImageFilter.Color3DLUT.generate((7, 9, 11), |
| 355 | + lambda r, g, b: (r, g, b)) |
| 356 | + lut.table = numpy.array(lut.table, dtype=numpy.int32) |
| 357 | + im.filter(lut) |
| 358 | + lut.table = numpy.array(lut.table, dtype=numpy.int8) |
| 359 | + im.filter(lut) |
| 360 | + |
282 | 361 | def test_repr(self): |
283 | 362 | lut = ImageFilter.Color3DLUT(2, [0, 1, 2] * 8) |
284 | 363 | self.assertEqual(repr(lut), |
|
0 commit comments