Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert PDF to PNG for OCR #381

Merged
merged 5 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions configs/python/backend/backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,14 @@ scanners:
- 'image/x-ms-bmp'
- 'bmp_file'
- 'image/webp'
- 'application/pdf'
- 'pdf_file'
priority: 5
options:
extract_text: False
split_words: True
tmp_directory: '/dev/shm/'
pdf_to_png: True
'ScanOle':
- positive:
flavors:
Expand Down
6 changes: 6 additions & 0 deletions src/python/strelka/scanners/scan_ocr.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fitz
alexk307 marked this conversation as resolved.
Show resolved Hide resolved
import os
import subprocess
import tempfile
Expand All @@ -20,6 +21,11 @@ def scan(self, data, file, options, expire_at):
extract_text = options.get("extract_text", False)
split_words = options.get("split_words", True)
tmp_directory = options.get("tmp_directory", "/tmp/")
pdf_to_png = options.get("pdf_to_png", False)

if pdf_to_png and "application/pdf" in file.flavors.get("mime", []):
doc = fitz.open(stream=data, filetype="pdf")
data = doc.get_page_pixmap(0).tobytes("png")

with tempfile.NamedTemporaryFile(dir=tmp_directory) as tmp_data:
tmp_data.write(data)
Expand Down