OCRmyPDF/tests/test_hocrtransform.py

45 lines
1.4 KiB
Python
Raw Normal View History

2016-01-15 14:14:08 -08:00
# © 2015 James R. Barlow: github.com/jbarlow83
#
# This file is part of OCRmyPDF.
#
# OCRmyPDF is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OCRmyPDF is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OCRmyPDF. If not, see <http://www.gnu.org/licenses/>.
2016-01-15 14:14:08 -08:00
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 PIL import Image
from pathlib import Path
2016-01-15 14:14:08 -08:00
import pytest
2018-06-23 01:47:53 -07:00
# pylint: disable=redefined-outer-name
2016-01-15 14:14:08 -08:00
@pytest.fixture
def blank_hocr(tmpdir):
filename = Path(str(tmpdir)) / "blank.hocr"
2018-06-23 00:54:22 -07:00
filename.write_text(HOCR_TEMPLATE) # pylint: disable=E1101
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
2017-01-26 17:53:27 -08:00
hocr = hocrtransform.HocrTransform(str(blank_hocr), 300)
2018-12-30 01:27:49 -08:00
hocr.to_pdf(str(outdir / 'mono.pdf'), imageFileName=str(outdir / 'mono.tif'))
2016-01-15 14:14:08 -08:00
qpdf.check(str(outdir / 'mono.pdf'))