为Tesseract优化图像质量
(self, img: Image.Image)
| 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 | """ |