Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save 1 mode PDF using CCITTFaxDecode filter #6470

Merged
merged 2 commits into from Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Tests/test_file_pdf.py
Expand Up @@ -43,7 +43,7 @@ def test_monochrome(tmp_path):

# Act / Assert
outfile = helper_save_as_pdf(tmp_path, mode)
assert os.path.getsize(outfile) < 15000
assert os.path.getsize(outfile) < 5000


def test_greyscale(tmp_path):
Expand Down
44 changes: 35 additions & 9 deletions src/PIL/PdfImagePlugin.py
Expand Up @@ -21,10 +21,11 @@
##

import io
import math
import os
import time

from . import Image, ImageFile, ImageSequence, PdfParser, __version__
from . import Image, ImageFile, ImageSequence, PdfParser, TiffImagePlugin, __version__

#
# --------------------------------------------------------------------
Expand Down Expand Up @@ -123,8 +124,26 @@ def _save(im, fp, filename, save_all=False):
params = None
decode = None

#
# Get image characteristics

width, height = im.size

if im.mode == "1":
filter = "DCTDecode"
filter = "CCITTFaxDecode"
bits = 1
params = PdfParser.PdfArray(
[
PdfParser.PdfDict(
{
"K": -1,
"BlackIs1": True,
"Columns": width,
"Rows": height,
}
)
]
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://opensource.adobe.com/dc-acrobat-sdk-docs/standards/pdfstandards/pdf/PDF32000_2008.pdf#page=38 indicates that K should be <0 for Group 4 encoding

On the next page of that document, it describes BlackIs1, that allows the colors to be inverted.

colorspace = PdfParser.PdfName("DeviceGray")
procset = "ImageB" # grayscale
elif im.mode == "L":
Expand Down Expand Up @@ -161,6 +180,11 @@ def _save(im, fp, filename, save_all=False):

if filter == "ASCIIHexDecode":
ImageFile._save(im, op, [("hex", (0, 0) + im.size, 0, im.mode)])
elif filter == "CCITTFaxDecode":
original_strip_size = TiffImagePlugin.STRIP_SIZE
TiffImagePlugin.STRIP_SIZE = math.ceil(im.width / 8) * im.height
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In writing this PR, I found that it was necessary to save the image as a single strip. This has actually been pointed out before, in #5740

im.save(op, "TIFF", compression="group4")
TiffImagePlugin.STRIP_SIZE = original_strip_size
elif filter == "DCTDecode":
Image.SAVE["JPEG"](im, op, filename)
elif filter == "FlateDecode":
Expand All @@ -170,22 +194,24 @@ def _save(im, fp, filename, save_all=False):
else:
raise ValueError(f"unsupported PDF filter ({filter})")

#
# Get image characteristics

width, height = im.size
stream = op.getvalue()
if filter == "CCITTFaxDecode":
stream = stream[8:]
filter = PdfParser.PdfArray([PdfParser.PdfName(filter)])
else:
filter = PdfParser.PdfName(filter)

existing_pdf.write_obj(
image_refs[page_number],
stream=op.getvalue(),
stream=stream,
Type=PdfParser.PdfName("XObject"),
Subtype=PdfParser.PdfName("Image"),
Width=width, # * 72.0 / resolution,
Height=height, # * 72.0 / resolution,
Filter=PdfParser.PdfName(filter),
Filter=filter,
BitsPerComponent=bits,
Decode=decode,
DecodeParams=params,
DecodeParms=params,
ColorSpace=colorspace,
)

Expand Down