Switched to saving 1-bit PDFs with DCTDecode

This commit is contained in:
Andrew Murray 2021-04-22 22:16:21 +10:00
parent ab8955b48e
commit d62be3e354
2 changed files with 3 additions and 8 deletions

View File

@ -42,7 +42,8 @@ def test_monochrome(tmp_path):
mode = "1" mode = "1"
# Act / Assert # Act / Assert
helper_save_as_pdf(tmp_path, mode) outfile = helper_save_as_pdf(tmp_path, mode)
assert os.path.getsize(outfile) < 15000
def test_greyscale(tmp_path): def test_greyscale(tmp_path):

View File

@ -124,7 +124,7 @@ def _save(im, fp, filename, save_all=False):
decode = None decode = None
if im.mode == "1": if im.mode == "1":
filter = "ASCIIHexDecode" filter = "DCTDecode"
colorspace = PdfParser.PdfName("DeviceGray") colorspace = PdfParser.PdfName("DeviceGray")
procset = "ImageB" # grayscale procset = "ImageB" # grayscale
bits = 1 bits = 1
@ -161,12 +161,6 @@ def _save(im, fp, filename, save_all=False):
op = io.BytesIO() op = io.BytesIO()
if filter == "ASCIIHexDecode": if filter == "ASCIIHexDecode":
if bits == 1:
# FIXME: the hex encoder doesn't support packed 1-bit
# images; do things the hard way...
data = im.tobytes("raw", "1")
im = Image.new("L", im.size)
im.putdata(data)
ImageFile._save(im, op, [("hex", (0, 0) + im.size, 0, im.mode)]) ImageFile._save(im, op, [("hex", (0, 0) + im.size, 0, im.mode)])
elif filter == "DCTDecode": elif filter == "DCTDecode":
Image.SAVE["JPEG"](im, op, filename) Image.SAVE["JPEG"](im, op, filename)