".github/vscode:/vscode.git/clone" did not exist on "632d506d0b526f641f9ced4f408dad8bd64b5009"
functional_pil.py 11.8 KB
Newer Older
1
import numbers
2
from typing import Any, Dict, List, Literal, Optional, Sequence, Tuple, Union
3

vfdev's avatar
vfdev committed
4
import numpy as np
5
import torch
6
from PIL import Image, ImageEnhance, ImageOps
vfdev's avatar
vfdev committed
7

8
9
10
11
try:
    import accimage
except ImportError:
    accimage = None
12
from . import _pil_constants
13
14
15


@torch.jit.unused
vfdev's avatar
vfdev committed
16
def _is_pil_image(img: Any) -> bool:
17
18
19
20
21
22
    if accimage is not None:
        return isinstance(img, (Image.Image, accimage.Image))
    else:
        return isinstance(img, Image.Image)


23
24
25
@torch.jit.unused
def get_dimensions(img: Any) -> List[int]:
    if _is_pil_image(img):
26
27
28
29
        if hasattr(img, "getbands"):
            channels = len(img.getbands())
        else:
            channels = img.channels
30
31
32
33
34
        width, height = img.size
        return [channels, height, width]
    raise TypeError(f"Unexpected type {type(img)}")


vfdev's avatar
vfdev committed
35
@torch.jit.unused
36
def get_image_size(img: Any) -> List[int]:
vfdev's avatar
vfdev committed
37
    if _is_pil_image(img):
38
        return list(img.size)
39
    raise TypeError(f"Unexpected type {type(img)}")
vfdev's avatar
vfdev committed
40
41


42
@torch.jit.unused
43
def get_image_num_channels(img: Any) -> int:
44
    if _is_pil_image(img):
45
46
47
48
        if hasattr(img, "getbands"):
            return len(img.getbands())
        else:
            return img.channels
49
    raise TypeError(f"Unexpected type {type(img)}")
50
51


52
@torch.jit.unused
53
def hflip(img: Image.Image) -> Image.Image:
54
    if not _is_pil_image(img):
55
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
56

57
    return img.transpose(_pil_constants.FLIP_LEFT_RIGHT)
58
59
60


@torch.jit.unused
61
def vflip(img: Image.Image) -> Image.Image:
62
    if not _is_pil_image(img):
63
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
64

65
    return img.transpose(_pil_constants.FLIP_TOP_BOTTOM)
66
67
68


@torch.jit.unused
69
def adjust_brightness(img: Image.Image, brightness_factor: float) -> Image.Image:
70
    if not _is_pil_image(img):
71
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
72
73
74
75
76
77
78

    enhancer = ImageEnhance.Brightness(img)
    img = enhancer.enhance(brightness_factor)
    return img


@torch.jit.unused
79
def adjust_contrast(img: Image.Image, contrast_factor: float) -> Image.Image:
80
    if not _is_pil_image(img):
81
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
82
83
84
85
86
87
88

    enhancer = ImageEnhance.Contrast(img)
    img = enhancer.enhance(contrast_factor)
    return img


@torch.jit.unused
89
def adjust_saturation(img: Image.Image, saturation_factor: float) -> Image.Image:
90
    if not _is_pil_image(img):
91
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
92
93
94
95
96
97
98

    enhancer = ImageEnhance.Color(img)
    img = enhancer.enhance(saturation_factor)
    return img


@torch.jit.unused
99
def adjust_hue(img: Image.Image, hue_factor: float) -> Image.Image:
100
    if not (-0.5 <= hue_factor <= 0.5):
101
        raise ValueError(f"hue_factor ({hue_factor}) is not in [-0.5, 0.5].")
102
103

    if not _is_pil_image(img):
104
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
105
106

    input_mode = img.mode
107
    if input_mode in {"L", "1", "I", "F"}:
108
109
        return img

110
    h, s, v = img.convert("HSV").split()
111
112
113

    np_h = np.array(h, dtype=np.uint8)
    # uint8 addition take cares of rotation across boundaries
114
    with np.errstate(over="ignore"):
115
        np_h += np.uint8(hue_factor * 255)
116
    h = Image.fromarray(np_h, "L")
117

118
    img = Image.merge("HSV", (h, s, v)).convert(input_mode)
119
    return img
120
121


122
@torch.jit.unused
123
124
125
126
127
128
def adjust_gamma(
    img: Image.Image,
    gamma: float,
    gain: float = 1.0,
) -> Image.Image:

129
    if not _is_pil_image(img):
130
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
131
132

    if gamma < 0:
133
        raise ValueError("Gamma should be a non-negative real number")
134
135

    input_mode = img.mode
136
    img = img.convert("RGB")
137
    gamma_map = [int((255 + 1 - 1e-3) * gain * pow(ele / 255.0, gamma)) for ele in range(256)] * 3
138
139
140
141
142
143
    img = img.point(gamma_map)  # use PIL's point-function to accelerate this part

    img = img.convert(input_mode)
    return img


144
@torch.jit.unused
145
146
147
148
def pad(
    img: Image.Image,
    padding: Union[int, List[int], Tuple[int, ...]],
    fill: Optional[Union[float, List[float], Tuple[float, ...]]] = 0,
149
    padding_mode: Literal["constant", "edge", "reflect", "symmetric"] = "constant",
150
151
) -> Image.Image:

152
    if not _is_pil_image(img):
153
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
154
155
156

    if not isinstance(padding, (numbers.Number, tuple, list)):
        raise TypeError("Got inappropriate padding arg")
157
    if fill is not None and not isinstance(fill, (numbers.Number, tuple, list)):
158
159
160
161
162
163
164
165
        raise TypeError("Got inappropriate fill arg")
    if not isinstance(padding_mode, str):
        raise TypeError("Got inappropriate padding_mode arg")

    if isinstance(padding, list):
        padding = tuple(padding)

    if isinstance(padding, tuple) and len(padding) not in [1, 2, 4]:
166
        raise ValueError(f"Padding must be an int or a 1, 2, or 4 element tuple, not a {len(padding)} element tuple")
167
168
169
170
171
172
173
174
175

    if isinstance(padding, tuple) and len(padding) == 1:
        # Compatibility with `functional_tensor.pad`
        padding = padding[0]

    if padding_mode not in ["constant", "edge", "reflect", "symmetric"]:
        raise ValueError("Padding mode should be either constant, edge, reflect or symmetric")

    if padding_mode == "constant":
176
        opts = _parse_fill(fill, img, name="fill")
177
178
        if img.mode == "P":
            palette = img.getpalette()
179
            image = ImageOps.expand(img, border=padding, **opts)
180
181
182
            image.putpalette(palette)
            return image

183
        return ImageOps.expand(img, border=padding, **opts)
184
185
186
187
188
189
190
191
192
193
194
195
    else:
        if isinstance(padding, int):
            pad_left = pad_right = pad_top = pad_bottom = padding
        if isinstance(padding, tuple) and len(padding) == 2:
            pad_left = pad_right = padding[0]
            pad_top = pad_bottom = padding[1]
        if isinstance(padding, tuple) and len(padding) == 4:
            pad_left = padding[0]
            pad_top = padding[1]
            pad_right = padding[2]
            pad_bottom = padding[3]

196
197
198
199
200
201
202
203
204
        p = [pad_left, pad_top, pad_right, pad_bottom]
        cropping = -np.minimum(p, 0)

        if cropping.any():
            crop_left, crop_top, crop_right, crop_bottom = cropping
            img = img.crop((crop_left, crop_top, img.width - crop_right, img.height - crop_bottom))

        pad_left, pad_top, pad_right, pad_bottom = np.maximum(p, 0)

205
        if img.mode == "P":
206
207
            palette = img.getpalette()
            img = np.asarray(img)
208
            img = np.pad(img, ((pad_top, pad_bottom), (pad_left, pad_right)), mode=padding_mode)
209
210
211
212
213
214
215
216
217
218
219
220
221
            img = Image.fromarray(img)
            img.putpalette(palette)
            return img

        img = np.asarray(img)
        # RGB image
        if len(img.shape) == 3:
            img = np.pad(img, ((pad_top, pad_bottom), (pad_left, pad_right), (0, 0)), padding_mode)
        # Grayscale image
        if len(img.shape) == 2:
            img = np.pad(img, ((pad_top, pad_bottom), (pad_left, pad_right)), padding_mode)

        return Image.fromarray(img)
vfdev's avatar
vfdev committed
222
223
224


@torch.jit.unused
225
226
227
228
229
230
231
232
def crop(
    img: Image.Image,
    top: int,
    left: int,
    height: int,
    width: int,
) -> Image.Image:

vfdev's avatar
vfdev committed
233
    if not _is_pil_image(img):
234
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
vfdev's avatar
vfdev committed
235
236

    return img.crop((left, top, left + width, top + height))
vfdev's avatar
vfdev committed
237
238
239


@torch.jit.unused
240
241
def resize(
    img: Image.Image,
242
    size: Union[List[int], int],
243
    interpolation: int = _pil_constants.BILINEAR,
244
245
) -> Image.Image:

vfdev's avatar
vfdev committed
246
    if not _is_pil_image(img):
247
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
248
    if not (isinstance(size, list) and len(size) == 2):
249
        raise TypeError(f"Got inappropriate size arg: {size}")
vfdev's avatar
vfdev committed
250

251
    return img.resize(tuple(size[::-1]), interpolation)
vfdev's avatar
vfdev committed
252
253
254


@torch.jit.unused
255
256
257
258
259
260
def _parse_fill(
    fill: Optional[Union[float, List[float], Tuple[float, ...]]],
    img: Image.Image,
    name: str = "fillcolor",
) -> Dict[str, Optional[Union[float, List[float], Tuple[float, ...]]]]:

261
    # Process fill color for affine transforms
262
    num_channels = get_image_num_channels(img)
vfdev's avatar
vfdev committed
263
264
    if fill is None:
        fill = 0
265
266
    if isinstance(fill, (int, float)) and num_channels > 1:
        fill = tuple([fill] * num_channels)
267
    if isinstance(fill, (list, tuple)):
268
269
270
        if len(fill) != num_channels:
            msg = "The number of elements in 'fill' does not match the number of channels of the image ({} != {})"
            raise ValueError(msg.format(len(fill), num_channels))
271
272

        fill = tuple(fill)
vfdev's avatar
vfdev committed
273

274
275
276
277
278
279
    if img.mode != "F":
        if isinstance(fill, (list, tuple)):
            fill = tuple(int(x) for x in fill)
        else:
            fill = int(fill)

280
    return {name: fill}
vfdev's avatar
vfdev committed
281
282
283


@torch.jit.unused
284
285
286
def affine(
    img: Image.Image,
    matrix: List[float],
287
    interpolation: int = _pil_constants.NEAREST,
288
    fill: Optional[Union[int, float, Sequence[int], Sequence[float]]] = None,
289
290
) -> Image.Image:

vfdev's avatar
vfdev committed
291
    if not _is_pil_image(img):
292
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
vfdev's avatar
vfdev committed
293
294

    output_size = img.size
295
    opts = _parse_fill(fill, img)
296
    return img.transform(output_size, _pil_constants.AFFINE, matrix, interpolation, **opts)
vfdev's avatar
vfdev committed
297
298
299


@torch.jit.unused
300
301
302
def rotate(
    img: Image.Image,
    angle: float,
303
    interpolation: int = _pil_constants.NEAREST,
304
305
    expand: bool = False,
    center: Optional[Tuple[int, int]] = None,
306
    fill: Optional[Union[int, float, Sequence[int], Sequence[float]]] = None,
307
308
) -> Image.Image:

vfdev's avatar
vfdev committed
309
    if not _is_pil_image(img):
310
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
vfdev's avatar
vfdev committed
311

312
    opts = _parse_fill(fill, img)
313
    return img.rotate(angle, interpolation, expand, center, **opts)
314
315
316


@torch.jit.unused
317
318
def perspective(
    img: Image.Image,
319
    perspective_coeffs: List[float],
320
    interpolation: int = _pil_constants.BICUBIC,
321
    fill: Optional[Union[int, float, Sequence[int], Sequence[float]]] = None,
322
323
) -> Image.Image:

324
    if not _is_pil_image(img):
325
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
326

327
    opts = _parse_fill(fill, img)
328

329
    return img.transform(img.size, _pil_constants.PERSPECTIVE, perspective_coeffs, interpolation, **opts)
330
331
332


@torch.jit.unused
333
def to_grayscale(img: Image.Image, num_output_channels: int) -> Image.Image:
334
    if not _is_pil_image(img):
335
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
336
337

    if num_output_channels == 1:
338
        img = img.convert("L")
339
    elif num_output_channels == 3:
340
        img = img.convert("L")
341
342
        np_img = np.array(img, dtype=np.uint8)
        np_img = np.dstack([np_img, np_img, np_img])
343
        img = Image.fromarray(np_img, "RGB")
344
    else:
345
        raise ValueError("num_output_channels should be either 1 or 3")
346
347

    return img
348
349
350


@torch.jit.unused
351
def invert(img: Image.Image) -> Image.Image:
352
    if not _is_pil_image(img):
353
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
354
355
356
357
    return ImageOps.invert(img)


@torch.jit.unused
358
def posterize(img: Image.Image, bits: int) -> Image.Image:
359
    if not _is_pil_image(img):
360
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
361
362
363
364
    return ImageOps.posterize(img, bits)


@torch.jit.unused
365
def solarize(img: Image.Image, threshold: int) -> Image.Image:
366
    if not _is_pil_image(img):
367
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
368
369
370
371
    return ImageOps.solarize(img, threshold)


@torch.jit.unused
372
def adjust_sharpness(img: Image.Image, sharpness_factor: float) -> Image.Image:
373
    if not _is_pil_image(img):
374
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
375
376
377
378
379
380
381

    enhancer = ImageEnhance.Sharpness(img)
    img = enhancer.enhance(sharpness_factor)
    return img


@torch.jit.unused
382
def autocontrast(img: Image.Image) -> Image.Image:
383
    if not _is_pil_image(img):
384
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
385
386
387
388
    return ImageOps.autocontrast(img)


@torch.jit.unused
389
def equalize(img: Image.Image) -> Image.Image:
390
    if not _is_pil_image(img):
391
        raise TypeError(f"img should be PIL Image. Got {type(img)}")
392
    return ImageOps.equalize(img)