MCPcopy
hub / github.com/IPADS-SAI/MobiAgent / _enhance_image_for_tesseract

Method _enhance_image_for_tesseract

utils/ocr_engine.py:243–271  ·  view source on GitHub ↗

为Tesseract优化图像质量

(self, img: Image.Image)

Source from the content-addressed store, hash-verified

241 return img
242
243 def _enhance_image_for_tesseract(self, img: Image.Image) -> Image.Image:
244 """为Tesseract优化图像质量"""
245 try:
246 # 转换为灰度图
247 if img.mode != 'L':
248 img = img.convert('L')
249
250 # 增加对比度
251 from PIL import ImageEnhance
252 enhancer = ImageEnhance.Contrast(img)
253 img = enhancer.enhance(1.5)
254
255 # 锐化
256 from PIL import ImageFilter
257 img = img.filter(ImageFilter.SHARPEN)
258
259 # 如果图像太小,放大
260 w, h = img.size
261 if min(w, h) < 100:
262 scale = 200 / min(w, h)
263 new_w = int(w * scale)
264 new_h = int(h * scale)
265 img = img.resize((new_w, new_h), Image.Resampling.LANCZOS)
266 logger.debug(f"为Tesseract放大小图像: {w}x{h} -> {new_w}x{new_h}")
267
268 return img
269 except Exception as e:
270 logger.error(f"图像增强失败: {e}")
271 return img
272
273 def run(self, img: Any) -> OCRResult:
274 """

Callers 1

runMethod · 0.95

Calls 2

debugMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected