MCPcopy
hub / github.com/sml2h3/ddddocr / preprocess_for_ocr

Method preprocess_for_ocr

ddddocr/preprocessing/image_processor.py:241–287  ·  view source on GitHub ↗

OCR预处理流水线 Args: image: 输入图像 target_height: 目标高度 enhance_contrast: 是否增强对比度 remove_noise: 是否去噪 Returns: 预处理后的图像 Raises: ImageProcessError: 当处理失败时

(image: Image.Image, target_height: int = 64, 
                          enhance_contrast: bool = True, 
                          remove_noise: bool = True)

Source from the content-addressed store, hash-verified

239
240 @staticmethod
241 def preprocess_for_ocr(image: Image.Image, target_height: int = 64,
242 enhance_contrast: bool = True,
243 remove_noise: bool = True) -> Image.Image:
244 """
245 OCR预处理流水线
246
247 Args:
248 image: 输入图像
249 target_height: 目标高度
250 enhance_contrast: 是否增强对比度
251 remove_noise: 是否去噪
252
253 Returns:
254 预处理后的图像
255
256 Raises:
257 ImageProcessError: 当处理失败时
258 """
259 try:
260 processed_image = image.copy()
261
262 # 处理PNG透明背景
263 if processed_image.mode == 'RGBA':
264 processed_image = png_rgba_black_preprocess(processed_image)
265
266 # 调整尺寸(保持宽高比)
267 original_width, original_height = processed_image.size
268 target_width = int(original_width * (target_height / original_height))
269 processed_image = ImageProcessor.resize_image(
270 processed_image, (target_width, target_height), keep_aspect_ratio=False
271 )
272
273 # 增强对比度
274 if enhance_contrast:
275 processed_image = ImageProcessor.enhance_contrast(processed_image, factor=1.2)
276
277 # 去噪
278 if remove_noise:
279 processed_image = ImageProcessor.remove_noise(processed_image, kernel_size=3)
280
281 # 转换为灰度图
282 processed_image = ImageProcessor.convert_to_grayscale(processed_image)
283
284 return processed_image
285
286 except Exception as e:
287 raise ImageProcessError(f"OCR预处理失败: {str(e)}") from e

Callers

nothing calls this directly

Calls 6

ImageProcessErrorClass · 0.85
resize_imageMethod · 0.80
enhance_contrastMethod · 0.80
remove_noiseMethod · 0.80
convert_to_grayscaleMethod · 0.80

Tested by

no test coverage detected