Discard alpha channel when triaging images
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with OCRmyPDF. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from functools import partial
|
||||
from collections.abc import Iterable
|
||||
from contextlib import suppress, contextmanager
|
||||
@@ -128,6 +130,15 @@ def is_file_writable(test_file):
|
||||
return True
|
||||
|
||||
|
||||
def discard_alpha(im):
|
||||
if im.mode in ('RGBA', 'LA'):
|
||||
fill_color = (1, 1, 1) if im.mode == 'RGBA' else 1
|
||||
backgd = Image.new(im.mode[:-1], im.size, fill_color)
|
||||
backgd.paste(im, im.split()[-1])
|
||||
im = backgd
|
||||
return im
|
||||
|
||||
|
||||
if sys.version_info[0:2] <= (3, 5):
|
||||
def universal_open(p, *args, **kwargs):
|
||||
"Work around Python 3.5's inability to open(pathlib.Path())"
|
||||
|
||||
@@ -19,6 +19,7 @@ from contextlib import suppress
|
||||
from shutil import copyfileobj
|
||||
from pathlib import Path
|
||||
from datetime import datetime, timezone
|
||||
from io import BytesIO
|
||||
import sys
|
||||
import os
|
||||
import shutil
|
||||
@@ -33,7 +34,7 @@ from ruffus import formatter, regex, Pipeline, suffix
|
||||
from .hocrtransform import HocrTransform
|
||||
from .pdfinfo import PdfInfo, Encoding, Colorspace
|
||||
from .pdfa import generate_pdfa_ps, encode_pdf_date
|
||||
from .helpers import re_symlink, is_iterable_notstr, page_number
|
||||
from .helpers import re_symlink, is_iterable_notstr, page_number, discard_alpha
|
||||
from .exec import ghostscript, tesseract, qpdf
|
||||
from .lib import fitz
|
||||
from .exceptions import PdfMergeFailedError, UnsupportedImageFormatError, \
|
||||
@@ -159,8 +160,14 @@ def triage_image_file(input_file, output_file, log, options):
|
||||
layout_fun = img2pdf.get_fixed_dpi_layout_fun(
|
||||
(options.image_dpi, options.image_dpi))
|
||||
with open(output_file, 'wb') as outf:
|
||||
im = Image.open(input_file)
|
||||
im_format = im.format
|
||||
im = discard_alpha(im)
|
||||
im_bio = BytesIO()
|
||||
im.save(im_bio, format=im_format)
|
||||
im_bio.seek(0)
|
||||
img2pdf.convert(
|
||||
input_file,
|
||||
im_bio,
|
||||
layout_fun=layout_fun,
|
||||
with_pdfrw=False,
|
||||
outputstream=outf)
|
||||
|
||||
+1
-1
@@ -849,7 +849,7 @@ def test_compression_preserved(spoof_tesseract_noop, ocrmypdf_exec,
|
||||
stdin=input_stream, env=spoof_tesseract_noop)
|
||||
out, err = p.communicate()
|
||||
|
||||
assert p.returncode == ExitCode.ok
|
||||
assert p.returncode == ExitCode.ok, err.decode('utf-8')
|
||||
|
||||
pdfinfo = PdfInfo(output_file)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user