Compare commits

..
2 Commits
Author SHA1 Message Date
James R. Barlow 1d09061130 Revert previous commit amd reject input images with alpha channel
Decided on this for simplicity of old release branch.

Modifies baiona.png by stripping
alpha, adds baiona_alpha which
includes the alpha.
2018-07-31 23:45:28 -07:00
James R. Barlow a2203b2447 Discard alpha channel when triaging images 2018-07-25 22:23:41 -04:00
6 changed files with 32 additions and 11 deletions
+6
View File
@@ -10,6 +10,12 @@ The OCRmyPDF package itself does not contain a public API, although it is fairly
replace: `#$1 <https://github.com/jbarlow83/OCRmyPDF/issues/$1>`_ replace: `#$1 <https://github.com/jbarlow83/OCRmyPDF/issues/$1>`_
v6.2.3
------
- Fix compatibility with img2pdf >= 0.3.0 by rejecting input images that have an alpha channel
v6.2.2 v6.2.2
------ ------
+3 -1
View File
@@ -15,6 +15,8 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with OCRmyPDF. If not, see <http://www.gnu.org/licenses/>. # along with OCRmyPDF. If not, see <http://www.gnu.org/licenses/>.
from PIL import Image
from functools import partial from functools import partial
from collections.abc import Iterable from collections.abc import Iterable
from contextlib import suppress, contextmanager from contextlib import suppress, contextmanager
@@ -103,7 +105,7 @@ def is_file_writable(test_file):
if p.is_symlink(): if p.is_symlink():
# Python 3.5 does not accept parameters for Path.resolve() and behaves # Python 3.5 does not accept parameters for Path.resolve() and behaves
# as if strict=True (throws an exception on failure). Python 3.6 # as if strict=True (throws an exception on failure). Python 3.6
# defaults to strict=False. This implements strict=False like behavior # defaults to strict=False. This implements strict=False like behavior
# for Python 3.5. # for Python 3.5.
if sys.version_info[0:2] <= (3, 5): if sys.version_info[0:2] <= (3, 5):
+15 -7
View File
@@ -19,6 +19,7 @@ from contextlib import suppress
from shutil import copyfileobj from shutil import copyfileobj
from pathlib import Path from pathlib import Path
from datetime import datetime, timezone from datetime import datetime, timezone
from io import BytesIO
import sys import sys
import os import os
import shutil import shutil
@@ -144,6 +145,13 @@ def triage_image_file(input_file, output_file, log, options):
"image was scanned and specify it using --image-dpi.") "image was scanned and specify it using --image-dpi.")
raise DpiError() raise DpiError()
if im.mode in ('RGBA', 'LA'):
log.error(
"The input image has an alpha channel. Remove the alpha "
"channel first."
)
raise UnsupportedImageFormatError()
if 'iccprofile' not in im.info: if 'iccprofile' not in im.info:
if im.mode == 'RGB': if im.mode == 'RGB':
log.info('Input image has no ICC profile, assuming sRGB') log.info('Input image has no ICC profile, assuming sRGB')
@@ -172,7 +180,7 @@ def triage_image_file(input_file, output_file, log, options):
def _pdf_guess_version(input_file, search_window=1024): def _pdf_guess_version(input_file, search_window=1024):
"""Try to find version signature at start of file. """Try to find version signature at start of file.
Not robust enough to deal with appended files. Not robust enough to deal with appended files.
Returns empty string if not found, indicating file is probably not PDF. Returns empty string if not found, indicating file is probably not PDF.
@@ -231,7 +239,7 @@ def repair_and_parse_pdf(
"output these files.) Use --output-type=pdf instead." "output these files.) Use --output-type=pdf instead."
) )
raise InputFileError() raise InputFileError()
if len(pdfinfo.pages) > 2000 and sys.version_info[0:2] <= (3, 5): if len(pdfinfo.pages) > 2000 and sys.version_info[0:2] <= (3, 5):
log.warning( log.warning(
"Performance regressions are known occur with Python 3.5 for " "Performance regressions are known occur with Python 3.5 for "
@@ -298,7 +306,7 @@ def is_ocr_required(pageinfo, log, options):
# We found a page with no images and no text. That means it may # We found a page with no images and no text. That means it may
# have vector art that the user wants to OCR. If we determined # have vector art that the user wants to OCR. If we determined
# lossless reconstruction is not possible then we have to rasterize # lossless reconstruction is not possible then we have to rasterize
# the image. So if OCR is being forced, take that to mean YES, go # the image. So if OCR is being forced, take that to mean YES, go
# ahead and rasterize. If not forced, then pretend there's no text # ahead and rasterize. If not forced, then pretend there's no text
# on the page at all so we don't lose anything. # on the page at all so we don't lose anything.
# This could be made smarter by explicitly searching for vector art. # This could be made smarter by explicitly searching for vector art.
@@ -884,7 +892,7 @@ def get_pdfmark(base_pdf, options):
except (KeyError, TypeError): except (KeyError, TypeError):
return '' return ''
pdfmark = {k: from_document_info(k) for k in pdfmark = {k: from_document_info(k) for k in
('/Title', '/Author', '/Keywords', '/Subject', '/CreationDate')} ('/Title', '/Author', '/Keywords', '/Subject', '/CreationDate')}
if options.title: if options.title:
pdfmark['/Title'] = options.title pdfmark['/Title'] = options.title
@@ -1075,7 +1083,7 @@ def merge_sidecars(
if txt_file: if txt_file:
with open(txt_file, 'r', encoding="utf-8") as in_: with open(txt_file, 'r', encoding="utf-8") as in_:
txt = in_.read() txt = in_.read()
# Tesseract v4 alpha started adding form feeds in # Tesseract v4 alpha started adding form feeds in
# commit aa6eb6b # commit aa6eb6b
# No obvious way to detect what binaries will do this, so # No obvious way to detect what binaries will do this, so
# for consistency just ignore its form feeds and insert our # for consistency just ignore its form feeds and insert our
@@ -1109,7 +1117,7 @@ def copy_final(
sys.stdout.flush() sys.stdout.flush()
else: else:
# At this point we overwrite the output_file specified by the user # At this point we overwrite the output_file specified by the user
# use copyfileobj because then we use open() to create the file and # use copyfileobj because then we use open() to create the file and
# get the appropriate umask, ownership, etc. # get the appropriate umask, ownership, etc.
with open(output_file, 'wb') as output_stream: with open(output_file, 'wb') as output_stream:
copyfileobj(input_stream, output_stream) copyfileobj(input_stream, output_stream)
@@ -1284,7 +1292,7 @@ def build_pipeline(options, work_folder, log, context):
output=os.path.join(work_folder, r'\1.rendered.pdf'), output=os.path.join(work_folder, r'\1.rendered.pdf'),
extras=[log, context]) extras=[log, context])
task_combine_layers.graphviz(fillcolor='"#00cc66"') task_combine_layers.graphviz(fillcolor='"#00cc66"')
task_combine_layers.active_if(options.pdf_renderer == 'hocr' or task_combine_layers.active_if(options.pdf_renderer == 'hocr' or
options.pdf_renderer == 'sandwich') options.pdf_renderer == 'sandwich')
# Tesseract OCR+PDF # Tesseract OCR+PDF
Binary file not shown.

Before

Width:  |  Height:  |  Size: 168 KiB

After

Width:  |  Height:  |  Size: 147 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 KiB

+8 -3
View File
@@ -829,6 +829,7 @@ def test_no_contents(spoof_tesseract_noop, resources, outpdf):
@pytest.mark.parametrize('image', [ @pytest.mark.parametrize('image', [
'baiona.png', 'baiona.png',
'baiona_gray.png', 'baiona_gray.png',
'baiona_alpha.png',
'congress.jpg' 'congress.jpg'
]) ])
def test_compression_preserved(spoof_tesseract_noop, ocrmypdf_exec, def test_compression_preserved(spoof_tesseract_noop, ocrmypdf_exec,
@@ -839,7 +840,6 @@ def test_compression_preserved(spoof_tesseract_noop, ocrmypdf_exec,
output_file = str(outpdf) output_file = str(outpdf)
im = Image.open(input_file) im = Image.open(input_file)
# Runs: ocrmypdf - output.pdf < testfile # Runs: ocrmypdf - output.pdf < testfile
with open(input_file, 'rb') as input_stream: with open(input_file, 'rb') as input_stream:
p_args = ocrmypdf_exec + [ p_args = ocrmypdf_exec + [
@@ -849,7 +849,12 @@ def test_compression_preserved(spoof_tesseract_noop, ocrmypdf_exec,
stdin=input_stream, env=spoof_tesseract_noop) stdin=input_stream, env=spoof_tesseract_noop)
out, err = p.communicate() out, err = p.communicate()
assert p.returncode == ExitCode.ok if im.mode in ('RGBA', 'LA'):
# If alpha image is input, expect an error
assert p.returncode != ExitCode.ok and b'alpha' in err
return
assert p.returncode == ExitCode.ok, err.decode('utf-8')
pdfinfo = PdfInfo(output_file) pdfinfo = PdfInfo(output_file)
@@ -894,7 +899,7 @@ def test_compression_changed(spoof_tesseract_noop, ocrmypdf_exec,
stdin=input_stream, env=spoof_tesseract_noop) stdin=input_stream, env=spoof_tesseract_noop)
out, err = p.communicate() out, err = p.communicate()
assert p.returncode == ExitCode.ok assert p.returncode == ExitCode.ok, err
pdfinfo = PdfInfo(output_file) pdfinfo = PdfInfo(output_file)