You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于图像内文本旋转图像恢复原始方向,保障OCR提取准确性

批量修正图片旋转方向的解决方案

一、Python自动化方案(适合整合进你的OCR程序)

借助OpenCV和Tesseract的方向检测能力,自动识别并修正旋转角度:

  1. 先安装依赖包:
pip install opencv-python pytesseract pillow

(需提前安装Tesseract OCR引擎并配置好环境变量)

  1. 批量处理代码:
import cv2
import pytesseract
from PIL import Image
import os

def fix_image_rotation(img_path, save_dir):
    # 读取图片并转灰度
    img = cv2.imread(img_path)
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # 用Tesseract检测旋转角度
    osd_result = pytesseract.image_to_osd(gray_img)
    rotate_angle = int(osd_result.split('Rotate: ')[1].split('\n')[0])
    
    # 旋转回正确方向
    pil_img = Image.open(img_path)
    if rotate_angle != 0:
        corrected_img = pil_img.rotate(-rotate_angle, expand=True)
    else:
        corrected_img = pil_img
    
    # 保存修正后的图片
    filename = os.path.basename(img_path)
    corrected_img.save(os.path.join(save_dir, filename))

# 配置路径并批量处理
input_folder = "你的原始图片目录"
output_folder = "修正后图片的保存目录"
os.makedirs(output_folder, exist_ok=True)

for file in os.listdir(input_folder):
    if file.lower().endswith(('.png', '.jpg', '.jpeg')):
        fix_image_rotation(os.path.join(input_folder, file), output_folder)

该代码会自动识别90/180/270度的旋转,将图片转回正确方向,确保右上角的目标字段处于OCR易识别的位置。

二、命令行工具方案(无需编程)

用ImageMagick的自动旋转功能批量处理:

  1. 安装ImageMagick后,打开命令行进入图片目录,执行:
# 创建保存目录
mkdir corrected_images
# 批量修正并保存
for file in *.png *.jpg *.jpeg; do convert "$file" -auto-orient "corrected_images/$file"; done

-auto-orient参数会读取图片的EXIF方向信息自动修正旋转,适合带拍摄元数据的图片。

三、GUI工具批量处理(适合非编程用户)

使用IrfanView操作:

  • 打开IrfanView,点击「文件」→「批量转换/重命名」
  • 添加目标图片目录
  • 在「高级选项」中勾选「自动旋转图像(根据EXIF)」
  • 设置输出目录后,点击「开始批量处理」
    如果图片无EXIF信息,可先预览单张确定旋转角度,再在「旋转/翻转」选项中批量设置对应角度进行修正。

内容的提问来源于stack exchange,提问作者Raaghav Rammohan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 00:19:58