def test_numpy_sources(self): table = numpy.ones((5, 6, 7, 3), dtype=numpy.float16) with self.assertRaisesRegex(ValueError, "should have either channels"): lut = ImageFilter.Color3DLUT((5, 6, 7), table) table = numpy.ones((7, 6, 5, 3), dtype=numpy.float16) lut = ImageFilter.Color3DLUT((5, 6, 7), table) self.assertIsInstance(lut.table, numpy.ndarray) self.assertEqual(lut.table.dtype, table.dtype) self.assertEqual(lut.table.shape, (table.size, )) table = numpy.ones((7 * 6 * 5, 3), dtype=numpy.float16) lut = ImageFilter.Color3DLUT((5, 6, 7), table) self.assertEqual(lut.table.shape, (table.size, )) table = numpy.ones((7 * 6 * 5 * 3), dtype=numpy.float16) lut = ImageFilter.Color3DLUT((5, 6, 7), table) self.assertEqual(lut.table.shape, (table.size, )) # Check application Image.new('RGB', (10, 10), 0).filter(lut) # Check copy table[0] = 33 self.assertEqual(lut.table[0], 1) # Check not copy table = numpy.ones((7 * 6 * 5 * 3), dtype=numpy.float16) lut = ImageFilter.Color3DLUT((5, 6, 7), table, _copy_table=False) table[0] = 33 self.assertEqual(lut.table[0], 33)
def test_numpy_sources(self): table = numpy.ones((5, 6, 7, 3), dtype=numpy.float16) with pytest.raises(ValueError, match="should have either channels"): lut = ImageFilter.Color3DLUT((5, 6, 7), table) table = numpy.ones((7, 6, 5, 3), dtype=numpy.float16) lut = ImageFilter.Color3DLUT((5, 6, 7), table) assert isinstance(lut.table, numpy.ndarray) assert lut.table.dtype == table.dtype assert lut.table.shape == (table.size, ) table = numpy.ones((7 * 6 * 5, 3), dtype=numpy.float16) lut = ImageFilter.Color3DLUT((5, 6, 7), table) assert lut.table.shape == (table.size, ) table = numpy.ones((7 * 6 * 5 * 3), dtype=numpy.float16) lut = ImageFilter.Color3DLUT((5, 6, 7), table) assert lut.table.shape == (table.size, ) # Check application Image.new("RGB", (10, 10), 0).filter(lut) # Check copy table[0] = 33 assert lut.table[0] == 1 # Check not copy table = numpy.ones((7 * 6 * 5 * 3), dtype=numpy.float16) lut = ImageFilter.Color3DLUT((5, 6, 7), table, _copy_table=False) table[0] = 33 assert lut.table[0] == 33
def test_wrong_args(self): with self.assertRaisesRegex(ValueError, "should be either an integer"): ImageFilter.Color3DLUT("small", [1]) with self.assertRaisesRegex(ValueError, "should be either an integer"): ImageFilter.Color3DLUT((11, 11), [1]) with self.assertRaisesRegex(ValueError, r"in \[2, 65\] range"): ImageFilter.Color3DLUT((11, 11, 1), [1]) with self.assertRaisesRegex(ValueError, r"in \[2, 65\] range"): ImageFilter.Color3DLUT((11, 11, 66), [1]) with self.assertRaisesRegex(ValueError, "table should have .+ items"): ImageFilter.Color3DLUT((3, 3, 3), [1, 1, 1]) with self.assertRaisesRegex(ValueError, "table should have .+ items"): ImageFilter.Color3DLUT((3, 3, 3), [[1, 1, 1]] * 2) with self.assertRaisesRegex(ValueError, "should have a length of 4"): ImageFilter.Color3DLUT((3, 3, 3), [[1, 1, 1]] * 27, channels=4) with self.assertRaisesRegex(ValueError, "should have a length of 3"): ImageFilter.Color3DLUT((2, 2, 2), [[1, 1]] * 8) with self.assertRaisesRegex(ValueError, "Only 3 or 4 output"): ImageFilter.Color3DLUT((2, 2, 2), [[1, 1]] * 8, channels=2)
def test_repr(self): lut = ImageFilter.Color3DLUT(2, [0, 1, 2] * 8) self.assertEqual(repr(lut), "<Color3DLUT from list size=2x2x2 channels=3>") 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), "<Color3DLUT from array size=3x4x5 channels=4 target_mode=YCbCr>")
def test_convert_table(self): lut = ImageFilter.Color3DLUT(2, [0, 1, 2] * 8) self.assertEqual(tuple(lut.size), (2, 2, 2)) self.assertEqual(lut.name, "Color 3D LUT") lut = ImageFilter.Color3DLUT((2, 2, 2), [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14), (15, 16, 17), (18, 19, 20), (21, 22, 23)]) self.assertEqual(tuple(lut.size), (2, 2, 2)) self.assertEqual(lut.table, list(range(24))) lut = ImageFilter.Color3DLUT((2, 2, 2), [(0, 1, 2, 3)] * 8, channels=4)
def test_repr(self): lut = ImageFilter.Color3DLUT(2, [0, 1, 2] * 8) assert repr(lut) == "<Color3DLUT from list size=2x2x2 channels=3>" lut = ImageFilter.Color3DLUT( (3, 4, 5), array("f", [0, 0, 0, 0] * (3 * 4 * 5)), channels=4, target_mode="YCbCr", _copy_table=False, ) assert ( repr(lut) == "<Color3DLUT from array size=3x4x5 channels=4 target_mode=YCbCr>")
def Distortion(self): size = list(input("Enter 3DLUT values : ").split(" ")) table_size = self._check_size(size) total = 1 for num in table_size: total *= num while True: table = [round(random.random(), 2) for i in range(0, 3 * total)] self.image.filter(ImageFilter.Color3DLUT(table_size, table)).show() if utility.savechanges(): self.image = self.image.filter( ImageFilter.Color3DLUT(table_size, table)) check = int(input("Enter 0 for exit : ")) if not check: break self.Options()
def test_convert_table(self): lut = ImageFilter.Color3DLUT(2, [0, 1, 2] * 8) assert tuple(lut.size) == (2, 2, 2) assert lut.name == "Color 3D LUT" # fmt: off lut = ImageFilter.Color3DLUT((2, 2, 2), [ (0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14), (15, 16, 17), (18, 19, 20), (21, 22, 23)]) # fmt: on assert tuple(lut.size) == (2, 2, 2) assert lut.table == list(range(24)) lut = ImageFilter.Color3DLUT((2, 2, 2), [(0, 1, 2, 3)] * 8, channels=4) assert tuple(lut.size) == (2, 2, 2) assert lut.table == list(range(4)) * 8
def read_lut(path_lut:Union[str,Path], num_channels:int=3): 'Read LUT from raw file. Assumes each line in a file is part of the lut table' with open(path_lut) as f: lut_raw = f.read().splitlines() size = round(len(lut_raw) ** (1/3)) row2val = lambda row: tuple([float(val) for val in row]) lut_table = [row2val(row.split(' ')) for row in lut_raw if is_3dlut_row(row.split(' '))] return ImageFilter.Color3DLUT(size, lut_table, num_channels)
def D_img(self, i): if i == 3: r = randint(2, 64) if i == 2: r = randint(15, 64) if i == 1: r = randint(2, 7) arr = [] for _ in range(3 * r**3): arr.append(random()) self.new_img = self.pilImage.filter(ImageFilter.Color3DLUT(r, arr)) self.canvas_image_paste(self.new_img)
def color_lut(input_image, output_image): image = Image.open(input_image) filtered_image = image.filter(ImageFilter.Color3DLUT(2, [0, 1, 2] * 8)) filtered_image.save(output_image)