OCRmyPDF/tests/test_hocrtransform.py

41 lines
958 B
Python
Raw Normal View History

2016-01-15 14:14:08 -08:00
#!/usr/bin/env python3
# © 2015 James R. Barlow: github.com/jbarlow83
from ocrmypdf import hocrtransform
from ocrmypdf.exec.tesseract import HOCR_TEMPLATE
from ocrmypdf.exec import qpdf
2016-01-15 14:14:08 -08:00
from reportlab.pdfgen.canvas import Canvas
from PIL import Image
from tempfile import NamedTemporaryFile
from contextlib import suppress
from pathlib import Path
2016-01-15 14:14:08 -08:00
import os
import shutil
import pytest
import img2pdf
import pytest
import sys
@pytest.fixture
def blank_hocr(tmpdir):
filename = Path(tmpdir) / "blank.hocr"
with open(filename, 'w') as f:
2016-01-15 14:14:08 -08:00
f.write(HOCR_TEMPLATE)
return filename
2016-01-15 14:14:08 -08:00
def test_mono_image(blank_hocr, outdir):
2016-01-15 14:14:08 -08:00
im = Image.new('1', (8, 8), 0)
for n in range(8):
im.putpixel((n, n), 1)
im.save(outdir / 'mono.tif', format='TIFF')
2016-01-15 14:14:08 -08:00
hocr = hocrtransform.HocrTransform(blank_hocr, 300)
hocr.to_pdf(str(outdir / 'mono.pdf'), imageFileName=outdir / 'mono.tif')
2016-01-15 14:14:08 -08:00
qpdf.check(str(outdir / 'mono.pdf'))
2016-01-15 14:14:08 -08:00