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

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Aug 1, 2022

Resolves #6453

The issue requests the CCITTTaxDecode filter be using to save 1 mode PDFs, to reduce file size.

}
)
]
)
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.

@@ -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

@@ -1684,7 +1684,8 @@ def _save(im, fp, filename):
stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8)
# aim for given strip size (64 KB by default) when using libtiff writer
if libtiff:
rows_per_strip = 1 if stride == 0 else min(STRIP_SIZE // stride, im.size[1])
im_strip_size = encoderinfo.get("strip_size", STRIP_SIZE)
Copy link
Member Author

Choose a reason for hiding this comment

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

Rather than updating STRIP_SIZE and needing to reset it afterwards, I've added "strip_size" as a TIFF encoder argument.

@hugovk hugovk merged commit ae14255 into python-pillow:main Aug 7, 2022
@radarhere radarhere deleted the pdf_ccittfaxdecode branch August 7, 2022 23:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When I convert TIFF to PDF, the PDF size is 10 times that of TIFF
2 participants