10 examples of 'opencv resize image' in Python

Every line of 'opencv resize image' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
26def resize_image(image,w,h):
27 image=cv2.resize(image,(w,h))
28 cv2.imwrite(Folder_name+"/Resize-"+str(w)+"*"+str(h)+Extension, image)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

141def resize_image(self, img, scale):
142 """
143 resize image and transform dimention to [batchsize, channel, height, width]
144 Parameters:
145 ----------
146 img: numpy array , height x width x channel,input image, channels in BGR order here
147 scale: float number, scale factor of resize operation
148 Returns:
149 -------
150 transformed image tensor , 1 x channel x height x width
151 """
152 height, width, channels = img.shape
153 new_height = int(height * scale) # resized new height
154 new_width = int(width * scale) # resized new width
155 new_dim = (new_width, new_height)
156 img_resized = cv2.resize(img, new_dim, interpolation=cv2.INTER_LINEAR) # resized image
157
158 return img_resized
38def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
39 # ref: https://stackoverflow.com/questions/44650888/resize-an-image-without-distortion-opencv
40
41 # initialize the dimensions of the image to be resized and
42 # grab the image size
43 dim = None
44 (h, w) = image.shape[:2]
45
46 # if both the width and height are None, then return the
47 # original image
48 if width is None and height is None:
49 return image
50
51 # check to see if the width is None
52 if width is None:
53 # calculate the ratio of the height and construct the
54 # dimensions
55 r = height / float(h)
56 dim = (int(w * r), height)
57
58 # otherwise, the height is None
59 else:
60 # calculate the ratio of the width and construct the
61 # dimensions
62 r = width / float(w)
63 dim = (width, int(h * r))
64
65 # resize the image
66 resized = cv2.resize(image, dim, interpolation = inter)
67
68 # return the resized image
69 return resized
48def scale_image(self, image, scale):
49
50 if self.settings.opencv_or_pil == 'PIL':
51 ow, oh = image.size
52 nw = ow * scale
53 nh = oh * scale
54 return image.resize((int(nw), int(nh)), Image.ANTIALIAS)
55
56 else:
57 oh, ow, channels = image.shape
58
59 nw = ow * scale
60 nh = oh * scale
61
62 # PERF return cv2.resize(image, (int(nw), int(nh)), interpolation=cv2.INTER_NEAREST)
63 return cv2.resize(image, (int(nw), int(nh)), interpolation=cv2.INTER_CUBIC)
100def resize(self, width, height):
101 thumbnail = cv.CreateImage(
102 (int(round(width, 0)), int(round(height, 0))),
103 self.image_depth,
104 self.image_channels
105 )
106 cv.Resize(self.image, thumbnail, cv.CV_INTER_AREA)
107 self.image = thumbnail
103def __resize_image(self, image):
104 self.input_height = image.shape[0]
105 if self.height is not None:
106 zoom = self.height / float(image.shape[0])
107 width = int(float(image.shape[1]) * zoom) + 1
108 image = cv2.resize(image, (width, self.height), interpolation=cv2.INTER_CUBIC)
109 return image
27def resizeimg(img, width, height):
28 #img = cv2.imread(path)
29 #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
30 img = cv2.resize(img, (width,height))
31
32 img = img.astype(float)
33 img = img * (2.0 / 255.0) - 1.0
34 return img
29def imresize(img, size, return_scale=False, interpolation='bilinear'):
30 """Resize image to a given size.
31
32 Args:
33 img (ndarray): The input image.
34 size (tuple): Target (w, h).
35 return_scale (bool): Whether to return `w_scale` and `h_scale`.
36 interpolation (str): Interpolation method, accepted values are
37 "nearest", "bilinear", "bicubic", "area", "lanczos".
38
39 Returns:
40 tuple or ndarray: (`resized_img`, `w_scale`, `h_scale`) or
41 `resized_img`.
42 """
43 h, w = img.shape[:2]
44 resized_img = cv2.resize(
45 img, size, interpolation=interp_codes[interpolation])
46 if not return_scale:
47 return resized_img
48 else:
49 w_scale = size[0] / w
50 h_scale = size[1] / h
51 return resized_img, w_scale, h_scale
676def apply_resize(image, resize_param):
677 assert isinstance(resize_param.interp_mode, (list, tuple))
678
679 interp_mode = cv2.INTER_LINEAR
680 num_interp_mode = len(resize_param.interp_mode)
681 if num_interp_mode > 0:
682 probs = [1. / num_interp_mode] * num_interp_mode
683 cumulative = np.cumsum(probs)
684 val = random.uniform(0, cumulative[-1])
685 prob_num = bisect.bisect_left(cumulative, val)
686 interp_mode = resize_param.interp_mode[prob_num]
687
688 if resize_param.resize_mode == ResizeParameter.WARP:
689 return cv2.resize(image, dsize=(resize_param.width, resize_param.height), interpolation=interp_mode)
690 elif resize_param.resize_mode == ResizeParameter.FIT_LARGE_SIZE_AND_PAD:
691 return aspect_keeping_resize_and_pad(image, resize_param.width, resize_param.height, resize_param.pad_mode,
692 resize_param.pad_val, interp_mode)
693 elif resize_param.resize_mode == ResizeParameter.FIT_SMALL_SIZE:
694 return aspect_keeping_resize_by_small(image, resize_param.width, resize_param.height, interp_mode)
695 raise Exception()
7def resize_img(img, scale_factor):
8 new_size = (np.floor(np.array(img.shape[0:2]) * scale_factor)).astype(int)
9 new_img = cv2.resize(img, (new_size[1], new_size[0]))
10 # This is scale factor of [height, width] i.e. [y, x]
11 actual_factor = [
12 new_size[0] / float(img.shape[0]), new_size[1] / float(img.shape[1])
13 ]
14 return new_img, actual_factor

Related snippets