From 3b4ea7c60275d5912c2954de00e444df4a841149 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 22 Aug 2022 19:57:33 +1000 Subject: [PATCH] Do not use CCITTFaxDecode filter if libtiff is not available --- Tests/test_file_pdf.py | 4 ++-- src/PIL/PdfImagePlugin.py | 33 ++++++++++++++++++--------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/Tests/test_file_pdf.py b/Tests/test_file_pdf.py index 310619fb2..b27dbeedd 100644 --- a/Tests/test_file_pdf.py +++ b/Tests/test_file_pdf.py @@ -6,7 +6,7 @@ import time import pytest -from PIL import Image, PdfParser +from PIL import Image, PdfParser, features from .helper import hopper, mark_if_feature_version @@ -43,7 +43,7 @@ def test_monochrome(tmp_path): # Act / Assert outfile = helper_save_as_pdf(tmp_path, mode) - assert os.path.getsize(outfile) < 5000 + assert os.path.getsize(outfile) < (5000 if features.check("libtiff") else 15000) def test_greyscale(tmp_path): diff --git a/src/PIL/PdfImagePlugin.py b/src/PIL/PdfImagePlugin.py index 181a05b8d..404759a7f 100644 --- a/src/PIL/PdfImagePlugin.py +++ b/src/PIL/PdfImagePlugin.py @@ -25,7 +25,7 @@ import math import os import time -from . import Image, ImageFile, ImageSequence, PdfParser, __version__ +from . import Image, ImageFile, ImageSequence, PdfParser, __version__, features # # -------------------------------------------------------------------- @@ -130,20 +130,23 @@ def _save(im, fp, filename, save_all=False): width, height = im.size if im.mode == "1": - filter = "CCITTFaxDecode" - bits = 1 - params = PdfParser.PdfArray( - [ - PdfParser.PdfDict( - { - "K": -1, - "BlackIs1": True, - "Columns": width, - "Rows": height, - } - ) - ] - ) + if features.check("libtiff"): + filter = "CCITTFaxDecode" + bits = 1 + params = PdfParser.PdfArray( + [ + PdfParser.PdfDict( + { + "K": -1, + "BlackIs1": True, + "Columns": width, + "Rows": height, + } + ) + ] + ) + else: + filter = "DCTDecode" colorspace = PdfParser.PdfName("DeviceGray") procset = "ImageB" # grayscale elif im.mode == "L":