Debian Codegraph @

imgviz/resize.py

raw symbols SCIP
1 import numpy as np
2 import PIL.Image
3
4 from . import utils
5
6 try:
7 import cv2
8 except ImportError:
9 cv2 = None # type: ignore
10
11
12 def _resize_pillow(src, height, width, interpolation):
13 if interpolation == "linear":
14 interpolation = PIL.Image.BILINEAR
15 elif interpolation == "nearest":
16 interpolation = PIL.Image.NEAREST
17 else:
18 raise ValueError("unsupported interpolation: {}".format(interpolation))
19
20 if np.issubdtype(src.dtype, np.integer):
21 dst = utils.numpy_to_pillow(src)
22 dst = dst.resize((width, height), resample=interpolation)
23 dst = utils.pillow_to_numpy(dst)
24 else:
25 assert np.issubdtype(src.dtype, np.floating)
26 ndim = src.ndim
27 if ndim == 2:
28 src = src[:, :, None]
29
30 C = src.shape[2]
31 dst = np.zeros((height, width, C), dtype=src.dtype)
32 for c in range(C):
33 src_c = src[:, :, c]
34 src_c = utils.numpy_to_pillow(src_c)
35 dst[:, :, c] = src_c.resize((width, height), resample=interpolation)
36
37 if ndim == 2:
38 dst = dst[:, :, 0]
39 return dst
40
41
42 def _resize_opencv(src, height, width, interpolation):
43 if interpolation == "linear":
44 interpolation = cv2.INTER_LINEAR
45 elif interpolation == "nearest":
46 interpolation = cv2.INTER_NEAREST
47 else:
48 raise ValueError("unsupported interpolation: {}".format(interpolation))
49
50 dst = cv2.resize(src, (width, height), interpolation=interpolation)
51 return dst
52
53
54 def resize(
55 src,
56 height=None,
57 width=None,
58 interpolation="linear",
59 backend="auto",
60 ):
61 """Resize image.
62
63 Parameters
64 ----------
65 src: numpy.ndarray, (H, W) or (H, W, C)
66 Input image.
67 height: int, optional
68 Height of image. If not given,
69 the image is resized based on width keeping image ratio.
70 width: int, optional
71 Width of image. If not given,
72 the image is resized based on height keeping image ratio.
73 interpolation: str
74 Resizing interpolation (default: 'linear').
75
76 'linear':
77 Linear interpolation.
78 'nearest':
79 Interpolate with the nearest value.
80
81 backend: str
82 Resizing backend (default: 'auto').
83
84 'pillow':
85 Pillow is used.
86 'opencv':
87 OpenCV is used.
88
89 Returns
90 -------
91 dst: numpy.ndarray
92 Resized image.
93
94 """
95 if not isinstance(src, np.ndarray):
96 raise TypeError("src type must be numpy.ndarray")
97
98 if backend == "auto":
99 backend = "pillow" if cv2 is None else "opencv"
100
101 src_height, src_width = src.shape[:2]
102 if isinstance(width, float):
103 scale_width = width
104 width = int(round(scale_width * src_width))
105 if isinstance(height, float):
106 scale_height = height
107 height = int(round(scale_height * src_height))
108 if height is None:
109 assert width is not None
110 scale_height = 1.0 * width / src_width
111 height = int(round(scale_height * src_height))
112 if width is None:
113 assert height is not None
114 scale_width = 1.0 * height / src_height
115 width = int(round(scale_width * src_width))
116
117 if backend == "pillow":
118 dst = _resize_pillow(src, height, width, interpolation)
119 elif backend == "opencv":
120 dst = _resize_opencv(src, height, width, interpolation)
121 else:
122 raise ValueError("unsupported backend: {}".format(backend))
123
124 return dst