mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-12 18:26:17 +03:00
Remove Color3DLUT.from_cube_file from сore library
This commit is contained in:
parent
76d467245d
commit
912980c52f
|
@ -297,104 +297,6 @@ class TestColorLut3DFilter(PillowTestCase):
|
|||
ImageFilter.Color3DLUT.generate(5, channels=4,
|
||||
callback=lambda r, g, b: (r, g, b))
|
||||
|
||||
def test_from_cube_file_minimal(self):
|
||||
lut = ImageFilter.Color3DLUT.from_cube_file([
|
||||
"LUT_3D_SIZE 2",
|
||||
"0 0 0.031",
|
||||
"0.96 0 0.031",
|
||||
"0 1 0.031",
|
||||
"0.96 1 0.031",
|
||||
"0 0 0.931",
|
||||
"0.96 0 0.931",
|
||||
"0 1 0.931",
|
||||
"0.96 1 0.931",
|
||||
])
|
||||
self.assertEqual(tuple(lut.size), (2, 2, 2))
|
||||
self.assertEqual(lut.name, "Color 3D LUT")
|
||||
self.assertEqual(lut.table[:12], [
|
||||
0, 0, 0.031, 0.96, 0, 0.031, 0, 1, 0.031, 0.96, 1, 0.031])
|
||||
|
||||
def test_from_cube_file_parser(self):
|
||||
lut = ImageFilter.Color3DLUT.from_cube_file([
|
||||
" # Comment",
|
||||
'TITLE "LUT name from file"',
|
||||
" LUT_3D_SIZE 2 3 4",
|
||||
" SKIP THIS",
|
||||
"",
|
||||
" # Comment",
|
||||
"CHANNELS 4",
|
||||
"",
|
||||
] + [
|
||||
" # Comment",
|
||||
"0 0 0.031 1",
|
||||
"0.96 0 0.031 1",
|
||||
"",
|
||||
"0 1 0.031 1",
|
||||
"0.96 1 0.031 1",
|
||||
] * 6, target_mode='HSV')
|
||||
self.assertEqual(tuple(lut.size), (2, 3, 4))
|
||||
self.assertEqual(lut.channels, 4)
|
||||
self.assertEqual(lut.name, "LUT name from file")
|
||||
self.assertEqual(lut.mode, 'HSV')
|
||||
self.assertEqual(lut.table[:12], [
|
||||
0, 0, 0.031, 1, 0.96, 0, 0.031, 1, 0, 1, 0.031, 1])
|
||||
|
||||
def test_from_cube_file_errors(self):
|
||||
with self.assertRaisesRegexp(ValueError, "No size found"):
|
||||
lut = ImageFilter.Color3DLUT.from_cube_file([
|
||||
'TITLE "LUT name from file"',
|
||||
] + [
|
||||
"0 0 0.031",
|
||||
"0.96 0 0.031",
|
||||
] * 3)
|
||||
|
||||
with self.assertRaisesRegexp(ValueError, "number of colors on line 3"):
|
||||
lut = ImageFilter.Color3DLUT.from_cube_file([
|
||||
'LUT_3D_SIZE 2',
|
||||
] + [
|
||||
"0 0 0.031",
|
||||
"0.96 0 0.031 1",
|
||||
] * 3)
|
||||
|
||||
with self.assertRaisesRegexp(ValueError, "1D LUT cube files"):
|
||||
lut = ImageFilter.Color3DLUT.from_cube_file([
|
||||
'LUT_1D_SIZE 2',
|
||||
] + [
|
||||
"0 0 0.031",
|
||||
"0.96 0 0.031 1",
|
||||
])
|
||||
|
||||
with self.assertRaisesRegexp(ValueError, "Not a number on line 2"):
|
||||
lut = ImageFilter.Color3DLUT.from_cube_file([
|
||||
'LUT_3D_SIZE 2',
|
||||
] + [
|
||||
"0 green 0.031",
|
||||
"0.96 0 0.031",
|
||||
] * 3)
|
||||
|
||||
def test_from_cube_file_filename(self):
|
||||
with NamedTemporaryFile('w+t', delete=False) as f:
|
||||
f.write(
|
||||
"LUT_3D_SIZE 2\n"
|
||||
"0 0 0.031\n"
|
||||
"0.96 0 0.031\n"
|
||||
"0 1 0.031\n"
|
||||
"0.96 1 0.031\n"
|
||||
"0 0 0.931\n"
|
||||
"0.96 0 0.931\n"
|
||||
"0 1 0.931\n"
|
||||
"0.96 1 0.931\n"
|
||||
)
|
||||
|
||||
try:
|
||||
lut = ImageFilter.Color3DLUT.from_cube_file(f.name)
|
||||
self.assertEqual(tuple(lut.size), (2, 2, 2))
|
||||
self.assertEqual(lut.name, "Color 3D LUT")
|
||||
self.assertEqual(lut.table[:12], [
|
||||
0, 0, 0.031, 0.96, 0, 0.031, 0, 1, 0.031, 0.96, 1, 0.031])
|
||||
finally:
|
||||
os.unlink(f.name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
@ -389,66 +389,6 @@ class Color3DLUT(MultibandFilter):
|
|||
|
||||
return cls((size1D, size2D, size3D), table, channels, target_mode)
|
||||
|
||||
@classmethod
|
||||
def from_cube_file(cls, lines, target_mode=None):
|
||||
name, size = None, None
|
||||
channels = 3
|
||||
file = None
|
||||
|
||||
if isPath(lines):
|
||||
file = lines = open(lines, 'rt')
|
||||
|
||||
try:
|
||||
iterator = iter(lines)
|
||||
|
||||
for i, line in enumerate(iterator, 1):
|
||||
line = line.strip()
|
||||
if line.startswith('TITLE "'):
|
||||
name = line.split('"')[1]
|
||||
continue
|
||||
if line.startswith('LUT_3D_SIZE '):
|
||||
size = [int(x) for x in line.split()[1:]]
|
||||
if len(size) == 1:
|
||||
size = size[0]
|
||||
continue
|
||||
if line.startswith('CHANNELS '):
|
||||
channels = int(line.split()[1])
|
||||
if line.startswith('LUT_1D_SIZE '):
|
||||
raise ValueError("1D LUT cube files aren't supported.")
|
||||
|
||||
try:
|
||||
float(line.partition(' ')[0])
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
# Data starts
|
||||
break
|
||||
|
||||
if size is None:
|
||||
raise ValueError('No size found in the file')
|
||||
|
||||
table = []
|
||||
for i, line in enumerate(chain([line], iterator), i):
|
||||
line = line.strip()
|
||||
if not line or line.startswith('#'):
|
||||
continue
|
||||
try:
|
||||
pixel = [float(x) for x in line.split()]
|
||||
except ValueError:
|
||||
raise ValueError("Not a number on line {}".format(i))
|
||||
if len(pixel) != channels:
|
||||
raise ValueError(
|
||||
"Wrong number of colors on line {}".format(i))
|
||||
table.append(tuple(pixel))
|
||||
finally:
|
||||
if file is not None:
|
||||
file.close()
|
||||
|
||||
instance = cls(size, table, channels, target_mode)
|
||||
if name is not None:
|
||||
instance.name = name
|
||||
return instance
|
||||
|
||||
def filter(self, image):
|
||||
from . import Image
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user