Fix non-square image resolution for "hocr" case; use img2pdf 0.2.1

Tesseract renderer not immediately fixable.
This commit is contained in:
James R. Barlow
2016-07-28 16:43:51 -07:00
parent b3fcf24a26
commit 8f77576dc4
4 changed files with 33 additions and 11 deletions
+13 -11
View File
@@ -637,10 +637,14 @@ def rasterize_with_ghostscript(
device = 'pnggray'
log.debug("Rasterize {0} with {1}".format(
os.path.basename(input_file), device))
os.path.basename(input_file), device))
xres, yres = get_page_dpi(pageinfo)
ghostscript.rasterize_pdf(input_file, output_file, xres, yres, device, log)
# Produce the page image with square resolution or else deskew and OCR
# will not work properly
dpi = get_page_square_dpi(pageinfo)
ghostscript.rasterize_pdf(
input_file, output_file, xres=dpi, yres=dpi, raster_device=device,
log=log)
@transform(
@@ -767,18 +771,16 @@ def select_image_layer(
re_symlink(page_pdf, output_file)
else:
pageinfo = get_pageinfo(image, pdfinfo, pdfinfo_lock)
dpi = round(get_page_square_dpi(pageinfo))
imgsize = ((img2pdf.ImgSize.dpi, dpi), (img2pdf.ImgSize.dpi, dpi))
layout_fun = img2pdf.get_layout_fun(None, imgsize, None, None, None)
dpi = get_page_dpi(pageinfo)
dpi = float(dpi[0]), float(dpi[1])
layout_fun = img2pdf.get_fixed_dpi_layout_fun(dpi)
with open(image, 'rb') as imfile, \
open(output_file, 'wb') as pdf:
rawdata = imfile.read()
pdf.write(img2pdf.convert(
rawdata, producer="img2pdf", with_pdfrw=False,
layout_fun=layout_fun))
img2pdf.convert(
rawdata, with_pdfrw=False,
layout_fun=layout_fun, outputstream=pdf)
@active_if(options.pdf_renderer == 'hocr')
+1
View File
@@ -31,6 +31,7 @@ Files generated for this project
The following test resources were crafted specifically for this project, and can be used
under the terms of the license in LICENSE.rst.
- aspect.pdf (a page with an image with 200 x 100 DPI resolution)
- blank.pdf (a blank PDF page)
- cmyk.pdf (a CMYK image created in Photoshop)
- enormous.pdf (a very lage page)
Binary file not shown.
+19
View File
@@ -562,3 +562,22 @@ def test_old_unpaper():
def test_algo4():
sh, _, _ = run_ocrmypdf_env('encrypted_algo4.pdf', 'wontwork.pdf')
assert sh.returncode == ExitCode.encrypted_pdf
@pytest.mark.parametrize('renderer', [
'hocr'])
def test_non_square_resolution(renderer):
# Confirm input image is non-square resolution
in_pageinfo = pdf_get_all_pageinfo(_infile('aspect.pdf'))
assert in_pageinfo[0]['xres'] != in_pageinfo[0]['yres']
out = 'aspect_%s.pdf' % renderer
check_ocrmypdf(
'aspect.pdf', out,
'--pdf-renderer', renderer)
out_pageinfo = pdf_get_all_pageinfo(_outfile(out))
# Confirm resolution was kept the same
assert in_pageinfo[0]['xres'] == out_pageinfo[0]['xres']
assert in_pageinfo[0]['yres'] == out_pageinfo[0]['yres']