Skip to content

master

Master

Bases: Palette

Source code in phomo/master.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Master(Palette):
    @classmethod
    def from_file(
        cls,
        master_image_file: PathLike,
        crop_ratio: Optional[float] = None,
        img_size: Optional[Tuple[int, int]] = None,
        mode: Optional[str] = None,
    ) -> "Master":
        """Create a master image from file.

        Args:
            master_image_file: path to image file.
            crop_ratio: width to height ratio to crop the master image to. 1 results in a square image.
            img_size: resize the image to the provided size, width followed by height.
            mode: convert the image to the provided mode. See PIL Modes.

        Returns:
            Master image instance.

        Examples:
            For black and white square 1280x1280 image.

            >>> Master.from_file("master.png", crop_ratio=1, img_size=(1280, 1280), convert="L")
        """
        img = open_img_file(
            master_image_file, crop_ratio=crop_ratio, size=img_size, mode=mode
        )
        return cls.from_image(img)

    @classmethod
    def from_image(cls, master_image: Image.Image) -> "Master":
        """Create a master image from PIL.Image.Image

        Args:
            master_image: `PIL.Image` instance.

        Returns:
            Master image instance.
        """
        array = np.asarray(master_image)
        # make sure the arrays have 3 channels even in black and white
        if array.ndim == 2:
            array = np.stack([array] * 3, -1)
        return cls(array)

    def __init__(self, array: np.ndarray) -> None:
        """The master image.

        Args:
            array: numpy array of the image, should contain 3 channels.

        Returns:
            Master image instance.
        """
        super().__init__(array)

    @property
    def img(self):
        """`PIL.Image` of the master image."""
        return Image.fromarray(self.array.round(0).astype("uint8"), mode="RGB")

    @property
    def pixels(self) -> np.ndarray:
        """Array containing the 3-channel pixel values of the master image."""
        return self.array.reshape(-1, self.array.shape[-1])

    def __repr__(self) -> str:
        return f"""{self.__class__.__module__}.{self.__class__.__name__} at {hex(id(self))}:
    shape: {self.array.shape}"""

img property

PIL.Image of the master image.

pixels: np.ndarray property

Array containing the 3-channel pixel values of the master image.

__init__(array)

The master image.

Parameters:

Name Type Description Default
array ndarray

numpy array of the image, should contain 3 channels.

required

Returns:

Type Description
None

Master image instance.

Source code in phomo/master.py
60
61
62
63
64
65
66
67
68
69
def __init__(self, array: np.ndarray) -> None:
    """The master image.

    Args:
        array: numpy array of the image, should contain 3 channels.

    Returns:
        Master image instance.
    """
    super().__init__(array)

from_file(master_image_file, crop_ratio=None, img_size=None, mode=None) classmethod

Create a master image from file.

Parameters:

Name Type Description Default
master_image_file PathLike

path to image file.

required
crop_ratio Optional[float]

width to height ratio to crop the master image to. 1 results in a square image.

None
img_size Optional[Tuple[int, int]]

resize the image to the provided size, width followed by height.

None
mode Optional[str]

convert the image to the provided mode. See PIL Modes.

None

Returns:

Type Description
Master

Master image instance.

Examples:

For black and white square 1280x1280 image.

>>> Master.from_file("master.png", crop_ratio=1, img_size=(1280, 1280), convert="L")
Source code in phomo/master.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@classmethod
def from_file(
    cls,
    master_image_file: PathLike,
    crop_ratio: Optional[float] = None,
    img_size: Optional[Tuple[int, int]] = None,
    mode: Optional[str] = None,
) -> "Master":
    """Create a master image from file.

    Args:
        master_image_file: path to image file.
        crop_ratio: width to height ratio to crop the master image to. 1 results in a square image.
        img_size: resize the image to the provided size, width followed by height.
        mode: convert the image to the provided mode. See PIL Modes.

    Returns:
        Master image instance.

    Examples:
        For black and white square 1280x1280 image.

        >>> Master.from_file("master.png", crop_ratio=1, img_size=(1280, 1280), convert="L")
    """
    img = open_img_file(
        master_image_file, crop_ratio=crop_ratio, size=img_size, mode=mode
    )
    return cls.from_image(img)

from_image(master_image) classmethod

Create a master image from PIL.Image.Image

Parameters:

Name Type Description Default
master_image Image

PIL.Image instance.

required

Returns:

Type Description
Master

Master image instance.

Source code in phomo/master.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
@classmethod
def from_image(cls, master_image: Image.Image) -> "Master":
    """Create a master image from PIL.Image.Image

    Args:
        master_image: `PIL.Image` instance.

    Returns:
        Master image instance.
    """
    array = np.asarray(master_image)
    # make sure the arrays have 3 channels even in black and white
    if array.ndim == 2:
        array = np.stack([array] * 3, -1)
    return cls(array)