Skip to content

Settings

Crop dataclass

Crop a rectangular region from an image.

Usage:

# crop an image using coords
crop = Crop(Coord(10, 10, 50, 50))
Source code in nucleus/sdk/processing/image/settings.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@dataclass(slots=True)
class Crop:
    """Crop a rectangular region from an image.

    Usage:

        # crop an image using coords
        crop = Crop(Coord(10, 10, 50, 50))
    """

    box: Coord

    def __iter__(self):
        yield 'box', (
            self.box.left,
            self.box.top,
            self.box.right,
            self.box.bottom,
        )

Thumbnail dataclass

Resize the image into a thumbnail.

Usage:

# thumbnail size 50x50 pixels
thumb = Thumbnail(50, 50)
Source code in nucleus/sdk/processing/image/settings.py
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
84
85
86
87
88
89
90
@dataclass(slots=True)
class Thumbnail:
    """Resize the image into a thumbnail.

    Usage:

        # thumbnail size 50x50 pixels
        thumb = Thumbnail(50, 50)
    """

    _size: Tuple[int, int] = field(init=False)
    _gap: float = field(init=False)
    _resample: Resampling = field(init=False)

    width: int
    height: int

    def __post_init__(self):
        self._gap = 2.0
        self._size = (self.width, self.height)
        self._resample = Resampling.BICUBIC

    def reducing_gap(self, gap: float):
        self._gap = gap

    def resample(self, resample: Resampling):
        self._resample = resample

    def __iter__(self):
        yield 'size', self._size
        yield 'resample', self._resample
        yield 'reducing_gap', self._gap

Resize dataclass

Resize the image to a given size.

Usage:

# new image size 100x100
resize = Resize(100, 100)
Source code in nucleus/sdk/processing/image/settings.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
@dataclass(slots=True)
class Resize:
    """Resize the image to a given size.

    Usage:

        # new image size 100x100
        resize = Resize(100, 100)
    """

    _size: Tuple[int, int] = field(init=False)
    _box: Tuple[int, int, int, int] = field(init=False)
    _resample: Resampling = field(init=False)

    width: int
    height: int

    def __post_init__(self):
        self._size = (self.width, self.height)
        self._box = (0, 0, *self._size)
        self._resample = Resampling.BICUBIC

    def crop(self, box: Coord):
        self._box = (box.left, box.top, box.right, box.bottom)

    def resample(self, resample: Resampling):
        self._resample = resample

    def __iter__(self):
        yield 'size', self._size
        yield 'resample', self._resample
        yield 'box', self._box

Coordinate System

"The Python Imaging Library uses a Cartesian pixel coordinate system, with (0,0) in the upper left corner. Note that the coordinates refer to the implied pixel corners; the centre of a pixel addressed as (0, 0) actually lies at (0.5, 0.5)." - pillow

Coord dataclass

Represents a cartesian pixel coordinate.

Usage:

# two points in the cartesian plane: top + left, right + bottom
coord = Coord(10, 10, 50, 50) # left, top, right, bottom coordinates
Source code in nucleus/sdk/processing/image/settings.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@dataclass(slots=True)
class Coord:
    """Represents a cartesian pixel coordinate.

    Usage:

        # two points in the cartesian plane: top + left, right + bottom
        coord = Coord(10, 10, 50, 50) # left, top, right, bottom coordinates
    """

    left: int
    top: int
    right: int
    bottom: int

Note

During processing time, the setting classes are parsed as a method to dynamically call pillow image object. To know more about the settings implemented in this reference please see pillow docs.