10 examples of 'cv2 resize image' in Python

Every line of 'cv2 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
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
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

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)
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)
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
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
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
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
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
440def resize_image(img, min_side=800, max_side=1333):
441 """ Resize an image such that the size is constrained to min_side and max_side.
442
443 Args
444 min_side: The image's min side will be equal to min_side after resizing.
445 max_side: If after resizing the image's max side is above max_side, resize until the max side is
446 equal to max_side.
447
448 Returns
449 A resized image.
450 """
451 (rows, cols, _) = img.shape
452
453 smallest_side = min(rows, cols)
454
455 # rescale the image so the smallest side is min_side
456 scale = min_side / smallest_side
457
458 # check if the largest side is now greater than max_side, which can happen
459 # when images have a large aspect ratio
460 largest_side = max(rows, cols)
461 if largest_side * scale > max_side:
462 scale = max_side / largest_side
463
464 # resize the image with the computed scale
465 img = cv2.resize(img, None, fx=scale, fy=scale)
466
467 return img, scale
49def _resize(frame):
50 """Resizing function utilizing OpenCV.
51
52 Args:
53 frame: A frame from a cv2.video_reader object to process
54
55 Returns:
56 resized_frame: the frame, resized
57 """
58 resized_frame = cv2.resize(frame,
59 None,
60 fx=RESIZE_FACTOR,
61 fy=RESIZE_FACTOR,
62 interpolation=cv2.INTER_AREA)
63
64 return resized_frame

Related snippets