MCPcopy Index your code
hub / github.com/PDFMathTranslate/PDFMathTranslate / resize_and_pad_image

Method resize_and_pad_image

pdf2zh/doclayout.py:92–130  ·  view source on GitHub ↗

Resize and pad the image to the specified size, ensuring dimensions are multiples of stride. Parameters: - image: Input image - new_shape: Target size (integer or (height, width) tuple) - stride: Padding alignment stride, default 32 Returns:

(self, image, new_shape)

Source from the content-addressed store, hash-verified

90 return self._stride
91
92 def resize_and_pad_image(self, image, new_shape):
93 """
94 Resize and pad the image to the specified size, ensuring dimensions are multiples of stride.
95
96 Parameters:
97 - image: Input image
98 - new_shape: Target size (integer or (height, width) tuple)
99 - stride: Padding alignment stride, default 32
100
101 Returns:
102 - Processed image
103 """
104 if isinstance(new_shape, int):
105 new_shape = (new_shape, new_shape)
106
107 h, w = image.shape[:2]
108 new_h, new_w = new_shape
109
110 # Calculate scaling ratio
111 r = min(new_h / h, new_w / w)
112 resized_h, resized_w = int(round(h * r)), int(round(w * r))
113
114 # Resize image
115 image = cv2.resize(
116 image, (resized_w, resized_h), interpolation=cv2.INTER_LINEAR
117 )
118
119 # Calculate padding size and align to stride multiple
120 pad_w = (new_w - resized_w) % self.stride
121 pad_h = (new_h - resized_h) % self.stride
122 top, bottom = pad_h // 2, pad_h - pad_h // 2
123 left, right = pad_w // 2, pad_w - pad_w // 2
124
125 # Add padding
126 image = cv2.copyMakeBorder(
127 image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=(114, 114, 114)
128 )
129
130 return image
131
132 def scale_boxes(self, img1_shape, boxes, img0_shape):
133 """

Callers 2

predictMethod · 0.95

Calls

no outgoing calls

Tested by 1