Skip to content
This repository was archived by the owner on Nov 22, 2019. It is now read-only.

Commit c71ac6d

Browse files
author
Marie
committed
Factorize matrix string function
1 parent 7d8c2d2 commit c71ac6d

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

lutLab/rgb_to_xyz_matrix.py

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from utils.colors_helper import get_RGB_to_XYZ_matrix
1010
from utils.colorspaces import COLORSPACES
1111
from utils.private_colorspaces import PRIVATE_COLORSPACES
12+
from utils.matrix_helper import matrix_to_string, matrix_to_spimtx_string
1213
import argparse
1314
import sys
1415
from utils import debug_helper
@@ -24,34 +25,6 @@ class RGBToXYZMatrixException(Exception):
2425
pass
2526

2627

27-
def matrix_to_string(matrix, extra=""):
28-
"""Return a string version of the matrix
29-
30-
Args:
31-
matrix (numpy.matrix (3x3)): matrix to convert
32-
extra (float): additionnal 4th column value
33-
34-
Returns:
35-
string
36-
37-
"""
38-
return ("{0:.10f} {1:.10f} {2:.10f} {9}\n"
39-
"{3:.10f} {4:.10f} {5:.10f} {10}\n"
40-
"{6:.10f} {7:.10f} {8:.10f} {11} \n").format(matrix.item(0, 0),
41-
matrix.item(0, 1),
42-
matrix.item(0, 2),
43-
matrix.item(1, 0),
44-
matrix.item(1, 1),
45-
matrix.item(1, 2),
46-
matrix.item(2, 0),
47-
matrix.item(2, 1),
48-
matrix.item(2, 2),
49-
extra,
50-
extra,
51-
extra)
52-
53-
54-
def display_matrix(colorspace, matrix_format):
5528
"""Display RGB to XYZ matrix corresponding to colorspace and formatting
5629
as format
5730
@@ -73,8 +46,8 @@ def display_matrix(colorspace, matrix_format):
7346
matrix_dump = matrix_to_string(matrix)
7447
inv_matrix_dump = matrix_to_string(matrix.I)
7548
elif matrix_format == 'spimtx':
76-
matrix_dump = matrix_to_string(matrix, "0")
77-
inv_matrix_dump = matrix_to_string(matrix.I, "0")
49+
matrix_dump = matrix_to_spimtx_string(matrix)
50+
inv_matrix_dump = matrix_to_spimtx_string(matrix.I)
7851
else:
7952
matrix_dump = "{0}".format(matrix)
8053
inv_matrix_dump = "{0}".format(matrix.I)

utils/matrix_helper.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
""" String Matrix helper
2+
3+
.. moduleauthor:: `Marie FETIVEAU <github.com/mfe>`_
4+
5+
"""
6+
__version__ = "0.1"
7+
8+
9+
def matrix_to_string(matrix, red_offset=None, green_offset=None, blue_offset=None):
10+
"""Return a string version of the matrix
11+
12+
Args:
13+
matrix (numpy.matrix (3x3)): matrix to convert
14+
red_offset, green_offset, blue_offset (float): channel offset in 16bit int (spimtx format)
15+
16+
Returns:
17+
string
18+
19+
"""
20+
return ("{0:.10f} {1:.10f} {2:.10f} {9}\n"
21+
"{3:.10f} {4:.10f} {5:.10f} {10}\n"
22+
"{6:.10f} {7:.10f} {8:.10f} {11} \n").format(matrix.item(0, 0),
23+
matrix.item(0, 1),
24+
matrix.item(0, 2),
25+
matrix.item(1, 0),
26+
matrix.item(1, 1),
27+
matrix.item(1, 2),
28+
matrix.item(2, 0),
29+
matrix.item(2, 1),
30+
matrix.item(2, 2),
31+
red_offset is None and "" or red_offset,
32+
green_offset is None and "" or green_offset,
33+
blue_offset is None and "" or blue_offset)
34+
35+
36+
def matrix_to_spimtx_string(matrix, red_offset=0, green_offset=0, blue_offset=0):
37+
"""Return a spimtx string version of the matrix
38+
39+
Args:
40+
matrix (numpy.matrix (3x3)): matrix to convert
41+
red_offset, green_offset, blue_offset (float): channel offset in 16bit int
42+
43+
Returns:
44+
string
45+
46+
"""
47+
return matrix_to_string(matrix, red_offset, green_offset, blue_offset)
48+
49+
50+
def write_spimtx(matrix, file_path, red_offset=0, green_offset=0, blue_offset=0):
51+
f = open(file_path, 'w+')
52+
f.write(matrix_to_spimtx_string(matrix, red_offset, green_offset, blue_offset))
53+
f.close()

0 commit comments

Comments
 (0)