2.9 KiB
2.9 KiB
name, label, description, icon, requires, tabs
| name | label | description | icon | requires | tabs |
|---|---|---|---|---|---|
| ocr-extract | OCR 텍스트 추출 | Python pytesseract를 사용하여 이미지/스캔 문서에서 텍스트를 추출합니다. | \uE8D4 | python | cowork |
이미지 또는 스캔된 문서에서 텍스트를 추출하세요.
사전 준비
- Tesseract OCR 엔진이 시스템에 설치되어 있어야 합니다.
- Windows: https://github.com/UB-Mannheim/tesseract/wiki 에서 설치
- 한국어 지원: 설치 시 "Korean" 언어 데이터 선택
- Python 패키지 설치:
process_run: pip install pytesseract Pillow
작업 절차
- 이미지 확인: 사용자가 제공한 이미지 파일 확인
- 전처리 (선택): 이미지 품질이 낮으면 전처리 스크립트 적용
- OCR 실행: pytesseract로 텍스트 추출
- 결과 저장: 추출된 텍스트를 파일로 저장하고 사용자에게 안내
OCR 스크립트 템플릿
기본 텍스트 추출
import pytesseract
from PIL import Image
# Windows에서 Tesseract 경로 지정 (필요 시)
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img = Image.open('scan.png')
text = pytesseract.image_to_string(img, lang='kor+eng')
print(text)
with open('extracted.txt', 'w', encoding='utf-8') as f:
f.write(text)
이미지 전처리 (품질 개선)
from PIL import Image, ImageFilter, ImageEnhance
img = Image.open('scan.png')
# 그레이스케일 변환
img = img.convert('L')
# 대비 향상
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(2.0)
# 선명도 향상
img = img.filter(ImageFilter.SHARPEN)
# 이진화 (흑백)
threshold = 128
img = img.point(lambda x: 255 if x > threshold else 0)
img.save('preprocessed.png')
text = pytesseract.image_to_string(img, lang='kor+eng')
배치 OCR (여러 이미지)
import glob
import pytesseract
from PIL import Image
results = []
for path in sorted(glob.glob('*.png')):
img = Image.open(path)
text = pytesseract.image_to_string(img, lang='kor+eng')
results.append(f'--- {path} ---\n{text}\n')
with open('all_extracted.txt', 'w', encoding='utf-8') as f:
f.write('\n'.join(results))
표 영역 추출
import pytesseract
from PIL import Image
img = Image.open('table.png')
# TSV 형식으로 추출 (표 구조 보존)
tsv_data = pytesseract.image_to_data(img, lang='kor+eng', output_type=pytesseract.Output.DATAFRAME)
print(tsv_data)
지원 언어
kor— 한국어eng— 영어kor+eng— 한국어+영어 혼합 (권장)jpn— 일본어chi_sim— 중국어 간체
팁
- 스캔 해상도 300dpi 이상이면 인식률이 높습니다
- 기울어진 이미지는
img.rotate()로 보정 후 추출하세요 - 손글씨는 인식률이 낮을 수 있습니다
한국어로 안내하세요. 작업 폴더에 Python 스크립트와 결과 파일을 저장하세요.