Skip to content

Commit

Permalink
Implement support for pathlib.Path to various definitions performin…
Browse files Browse the repository at this point in the history
…g IO.
  • Loading branch information
KelSolaar committed Jan 28, 2024
1 parent 4023d57 commit ff4fe99
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 40 deletions.
8 changes: 6 additions & 2 deletions colour/io/ctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import subprocess
import tempfile
import textwrap
from pathlib import Path

import numpy as np

Expand Down Expand Up @@ -67,8 +68,8 @@

@required("ctlrender")
def ctl_render(
path_input: str,
path_output: str,
path_input: str | Path,
path_output: str | Path,
ctl_transforms: Sequence[str] | Dict[str, Sequence[str]],
*args: Any,
**kwargs: Any,
Expand Down Expand Up @@ -149,6 +150,9 @@ def ctl_render(
<BLANKLINE>
"""

path_input = str(path_input)
path_output = str(path_output)

if len(args) == 0:
args = ARGUMENTS_CTL_RENDER_DEFAULTS

Expand Down
17 changes: 11 additions & 6 deletions colour/io/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from __future__ import annotations

from dataclasses import dataclass, field
from pathlib import Path

import numpy as np

Expand Down Expand Up @@ -216,7 +217,7 @@ def convert_bit_depth(

@required("OpenImageIO")
def read_image_OpenImageIO(
path: str,
path: str | Path,
bit_depth: Literal[
"uint8", "uint16", "float16", "float32", "float64", "float128"
] = "float32",
Expand Down Expand Up @@ -296,7 +297,7 @@ def read_image_OpenImageIO(


def read_image_Imageio(
path: str,
path: str | Path,
bit_depth: Literal[
"uint8", "uint16", "float16", "float32", "float64", "float128"
] = "float32",
Expand Down Expand Up @@ -348,6 +349,8 @@ def read_image_Imageio(

from imageio import imread

path = str(path)

image = np.squeeze(imread(path, **kwargs))

return convert_bit_depth(image, bit_depth)
Expand All @@ -365,7 +368,7 @@ def read_image_Imageio(


def read_image(
path: str,
path: str | Path,
bit_depth: Literal[
"uint8", "uint16", "float16", "float32", "float64", "float128"
] = "float32",
Expand Down Expand Up @@ -447,7 +450,7 @@ def read_image(
@required("OpenImageIO")
def write_image_OpenImageIO(
image: ArrayLike,
path: str,
path: str | Path,
bit_depth: Literal[
"uint8", "uint16", "float16", "float32", "float64", "float128"
] = "float32",
Expand Down Expand Up @@ -585,7 +588,7 @@ def write_image_OpenImageIO(

def write_image_Imageio(
image: ArrayLike,
path: str,
path: str | Path,
bit_depth: Literal[
"uint8", "uint16", "float16", "float32", "float64", "float128"
] = "float32",
Expand Down Expand Up @@ -648,6 +651,8 @@ def write_image_Imageio(

from imageio import imwrite

path = str(path)

if all(
[
path.lower().endswith(".exr"),
Expand Down Expand Up @@ -676,7 +681,7 @@ def write_image_Imageio(

def write_image(
image: ArrayLike,
path: str,
path: str | Path,
bit_depth: Literal[
"uint8", "uint16", "float16", "float32", "float64", "float128"
] = "float32",
Expand Down
9 changes: 7 additions & 2 deletions colour/io/luts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from __future__ import annotations

import os
from pathlib import Path

from colour.hints import Any, LiteralLUTReadMethod, LiteralLUTWriteMethod
from colour.utilities import (
Expand Down Expand Up @@ -101,7 +102,7 @@


def read_LUT(
path: str,
path: str | Path,
method: LiteralLUTReadMethod | str | None = None,
**kwargs: Any,
) -> LUT1D | LUT3x1D | LUT3D | LUTSequence | LUTOperatorMatrix:
Expand Down Expand Up @@ -207,6 +208,8 @@ def read_LUT(
Offset : [ 0. 0. 0. 0.]
"""

path = str(path)

method = (
MAPPING_EXTENSION_TO_LUT_FORMAT[os.path.splitext(path)[-1]].lower()
if method is None
Expand Down Expand Up @@ -248,7 +251,7 @@ def read_LUT(

def write_LUT(
LUT: LUT1D | LUT3x1D | LUT3D | LUTSequence | LUTOperatorMatrix,
path: str,
path: str | Path,
decimals: int = 7,
method: LiteralLUTWriteMethod | str | None = None,
**kwargs: Any,
Expand Down Expand Up @@ -318,6 +321,8 @@ def write_LUT(
>>> write_LUT(LUT, "My_LUT.cube") # doctest: +SKIP
"""

path = str(path)

method = (
MAPPING_EXTENSION_TO_LUT_FORMAT[os.path.splitext(path)[-1]].lower()
if method is None
Expand Down
8 changes: 6 additions & 2 deletions colour/io/luts/cinespace_csp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

from __future__ import annotations

from pathlib import Path

import numpy as np

from colour.hints import ArrayLike, List, NDArrayFloat
Expand All @@ -43,7 +45,7 @@
]


def read_LUT_Cinespace(path: str) -> LUT3x1D | LUT3D | LUTSequence:
def read_LUT_Cinespace(path: str | Path) -> LUT3x1D | LUT3D | LUTSequence:
"""
Read given *Cinespace* *.csp* *LUT* file.
Expand Down Expand Up @@ -246,7 +248,7 @@ def _parse_table_section(lines):


def write_LUT_Cinespace(
LUT: LUT3x1D | LUT3D | LUTSequence, path: str, decimals: int = 7
LUT: LUT3x1D | LUT3D | LUTSequence, path: str | Path, decimals: int = 7
) -> bool:
"""
Write given *LUT* to given *Cinespace* *.csp* *LUT* file.
Expand Down Expand Up @@ -296,6 +298,8 @@ def write_LUT_Cinespace(
>>> write_LUT_Cinespace(LUT, "My_LUT.cube") # doctest: +SKIP
"""

path = str(path)

has_3D, has_3x1D = False, False

if isinstance(LUT, LUTSequence):
Expand Down
5 changes: 4 additions & 1 deletion colour/io/luts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import os
import re
from pathlib import Path

__author__ = "Colour Developers"
__copyright__ = "Copyright 2013 Colour Developers"
Expand All @@ -23,7 +24,7 @@
]


def path_to_title(path: str) -> str:
def path_to_title(path: str | Path) -> str:
"""
Convert given file path to title.
Expand All @@ -45,4 +46,6 @@ def path_to_title(path: str) -> str:
'Colour Correct'
"""

path = str(path)

return re.sub("_|-|\\.", " ", os.path.splitext(os.path.basename(path))[0])
10 changes: 8 additions & 2 deletions colour/io/luts/iridas_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

from __future__ import annotations

from pathlib import Path

import numpy as np

from colour.io.luts import LUT1D, LUT3D, LUT3x1D, LUTSequence
Expand All @@ -41,7 +43,7 @@
]


def read_LUT_IridasCube(path: str) -> LUT3x1D | LUT3D:
def read_LUT_IridasCube(path: str | Path) -> LUT3x1D | LUT3D:
"""
Read given *Iridas* *.cube* *LUT* file.
Expand Down Expand Up @@ -118,6 +120,8 @@ def read_LUT_IridasCube(path: str) -> LUT3x1D | LUT3D:
Comment 01 : Comments can go anywhere
"""

path = str(path)

title = path_to_title(path)
domain_min, domain_max = np.array([0, 0, 0]), np.array([1, 1, 1])
dimensions: int = 3
Expand Down Expand Up @@ -180,7 +184,7 @@ def read_LUT_IridasCube(path: str) -> LUT3x1D | LUT3D:


def write_LUT_IridasCube(
LUT: LUT3x1D | LUT3D | LUTSequence, path: str, decimals: int = 7
LUT: LUT3x1D | LUT3D | LUTSequence, path: str | Path, decimals: int = 7
) -> bool:
"""
Write given *LUT* to given *Iridas* *.cube* *LUT* file.
Expand Down Expand Up @@ -235,6 +239,8 @@ def write_LUT_IridasCube(
>>> write_LUT_IridasCube(LUTxD, "My_LUT.cube") # doctest: +SKIP
"""

path = str(path)

if isinstance(LUT, LUTSequence):
usage_warning(
f'"LUT" is a "LUTSequence" instance was passed, '
Expand Down
10 changes: 8 additions & 2 deletions colour/io/luts/resolve_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

from __future__ import annotations

from pathlib import Path

import numpy as np

from colour.io.luts import LUT1D, LUT3D, LUT3x1D, LUTSequence
Expand All @@ -42,7 +44,7 @@
]


def read_LUT_ResolveCube(path: str) -> LUT3x1D | LUT3D | LUTSequence:
def read_LUT_ResolveCube(path: str | Path) -> LUT3x1D | LUT3D | LUTSequence:
"""
Read given *Resolve* *.cube* *LUT* file.
Expand Down Expand Up @@ -160,6 +162,8 @@ def read_LUT_ResolveCube(path: str) -> LUT3x1D | LUT3D | LUTSequence:
Comment 04 : A second "LUT3D" comment.
"""

path = str(path)

title = path_to_title(path)
domain_3x1D, domain_3D = None, None
size_3x1D: int = 2
Expand Down Expand Up @@ -234,7 +238,7 @@ def read_LUT_ResolveCube(path: str) -> LUT3x1D | LUT3D | LUTSequence:

def write_LUT_ResolveCube(
LUT: LUT1D | LUT3x1D | LUT3D | LUTSequence,
path: str,
path: str | Path,
decimals: int = 7,
) -> bool:
"""
Expand Down Expand Up @@ -315,6 +319,8 @@ def write_LUT_ResolveCube(
>>> write_LUT_ResolveCube(LUT_sequence, "My_LUT.cube") # doctest: +SKIP
"""

path = str(path)

has_3D, has_3x1D = False, False

if isinstance(LUT, LUTSequence):
Expand Down
8 changes: 6 additions & 2 deletions colour/io/luts/sony_spi1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from __future__ import annotations

from pathlib import Path

import numpy as np

from colour.io.luts import LUT1D, LUT3x1D, LUTSequence
Expand All @@ -36,7 +38,7 @@
]


def read_LUT_SonySPI1D(path: str) -> LUT1D | LUT3x1D:
def read_LUT_SonySPI1D(path: str | Path) -> LUT1D | LUT3x1D:
"""
Read given *Sony* *.spi1d* *LUT* file.
Expand Down Expand Up @@ -154,7 +156,7 @@ def read_LUT_SonySPI1D(path: str) -> LUT1D | LUT3x1D:


def write_LUT_SonySPI1D(
LUT: LUT1D | LUT3x1D | LUTSequence, path: str, decimals: int = 7
LUT: LUT1D | LUT3x1D | LUTSequence, path: str | Path, decimals: int = 7
) -> bool:
"""
Write given *LUT* to given *Sony* *.spi1d* *LUT* file.
Expand Down Expand Up @@ -205,6 +207,8 @@ def write_LUT_SonySPI1D(
>>> write_LUT_SonySPI1D(LUT, "My_LUT.cube") # doctest: +SKIP
"""

path = str(path)

if isinstance(LUT, LUTSequence):
usage_warning(
f'"LUT" is a "LUTSequence" instance was passed, using first '
Expand Down
10 changes: 8 additions & 2 deletions colour/io/luts/sony_spi3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from __future__ import annotations

from pathlib import Path

import numpy as np

from colour.io.luts import LUT3D, LUTSequence
Expand All @@ -37,7 +39,7 @@
]


def read_LUT_SonySPI3D(path: str) -> LUT3D:
def read_LUT_SonySPI3D(path: str | Path) -> LUT3D:
"""
Read given *Sony* *.spi3d* *LUT* file.
Expand Down Expand Up @@ -90,6 +92,8 @@ def read_LUT_SonySPI3D(path: str) -> LUT3D:
Comment 01 : Adapted from a LUT generated by Foundry::LUT.
"""

path = str(path)

title = path_to_title(path)
domain_min, domain_max = np.array([0, 0, 0]), np.array([1, 1, 1])
size: int = 2
Expand Down Expand Up @@ -139,7 +143,7 @@ def read_LUT_SonySPI3D(path: str) -> LUT3D:


def write_LUT_SonySPI3D(
LUT: LUT3D | LUTSequence, path: str, decimals: int = 7
LUT: LUT3D | LUTSequence, path: str | Path, decimals: int = 7
) -> bool:
"""
Write given *LUT* to given *Sony* *.spi3d* *LUT* file.
Expand Down Expand Up @@ -177,6 +181,8 @@ def write_LUT_SonySPI3D(
>>> write_LUT_SonySPI3D(LUT, "My_LUT.cube") # doctest: +SKIP
"""

path = str(path)

if isinstance(LUT, LUTSequence):
usage_warning(
f'"LUT" is a "LUTSequence" instance was passed, using first '
Expand Down
Loading

0 comments on commit ff4fe99

Please sign in to comment.