what.utils.resize

 1import numpy as np
 2
 3def bilinear_resize(image, height, width):
 4  """
 5  `image` is a 2-D numpy array
 6  `height` and `width` are the desired spatial dimension of the new 2-D array.
 7  """
 8  img_height, img_width = image.shape
 9
10  image = image.ravel()
11
12  x_ratio = float(img_width - 1) / (width - 1) if width > 1 else 0
13  y_ratio = float(img_height - 1) / (height - 1) if height > 1 else 0
14
15  y, x = np.divmod(np.arange(height * width), width)
16
17  x_l = np.floor(x_ratio * x).astype('int32')
18  y_l = np.floor(y_ratio * y).astype('int32')
19
20  x_h = np.ceil(x_ratio * x).astype('int32')
21  y_h = np.ceil(y_ratio * y).astype('int32')
22
23  x_weight = (x_ratio * x) - x_l
24  y_weight = (y_ratio * y) - y_l
25
26  a = image[y_l * img_width + x_l]
27  b = image[y_l * img_width + x_h]
28  c = image[y_h * img_width + x_l]
29  d = image[y_h * img_width + x_h]
30
31  resized = a * (1 - x_weight) * (1 - y_weight) + \
32            b * x_weight * (1 - y_weight) + \
33            c * y_weight * (1 - x_weight) + \
34            d * x_weight * y_weight
35
36  return resized.reshape(height, width)
def bilinear_resize(image, height, width):
 4def bilinear_resize(image, height, width):
 5  """
 6  `image` is a 2-D numpy array
 7  `height` and `width` are the desired spatial dimension of the new 2-D array.
 8  """
 9  img_height, img_width = image.shape
10
11  image = image.ravel()
12
13  x_ratio = float(img_width - 1) / (width - 1) if width > 1 else 0
14  y_ratio = float(img_height - 1) / (height - 1) if height > 1 else 0
15
16  y, x = np.divmod(np.arange(height * width), width)
17
18  x_l = np.floor(x_ratio * x).astype('int32')
19  y_l = np.floor(y_ratio * y).astype('int32')
20
21  x_h = np.ceil(x_ratio * x).astype('int32')
22  y_h = np.ceil(y_ratio * y).astype('int32')
23
24  x_weight = (x_ratio * x) - x_l
25  y_weight = (y_ratio * y) - y_l
26
27  a = image[y_l * img_width + x_l]
28  b = image[y_l * img_width + x_h]
29  c = image[y_h * img_width + x_l]
30  d = image[y_h * img_width + x_h]
31
32  resized = a * (1 - x_weight) * (1 - y_weight) + \
33            b * x_weight * (1 - y_weight) + \
34            c * y_weight * (1 - x_weight) + \
35            d * x_weight * y_weight
36
37  return resized.reshape(height, width)

image is a 2-D numpy array height and width are the desired spatial dimension of the new 2-D array.