Skip to content

Commit ae14255

Browse files
authored
Merge pull request #6470 from radarhere/pdf_ccittfaxdecode
Save 1 mode PDF using CCITTFaxDecode filter
2 parents 38e411d + 2b14d83 commit ae14255

4 files changed

Lines changed: 48 additions & 14 deletions

File tree

Tests/test_file_libtiff.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,14 +1011,18 @@ def test_save_multistrip(self, compression, tmp_path):
10111011
# Assert that there are multiple strips
10121012
assert len(im.tag_v2[STRIPOFFSETS]) > 1
10131013

1014-
def test_save_single_strip(self, tmp_path):
1014+
@pytest.mark.parametrize("argument", (True, False))
1015+
def test_save_single_strip(self, argument, tmp_path):
10151016
im = hopper("RGB").resize((256, 256))
10161017
out = str(tmp_path / "temp.tif")
10171018

1018-
TiffImagePlugin.STRIP_SIZE = 2**18
1019+
if not argument:
1020+
TiffImagePlugin.STRIP_SIZE = 2**18
10191021
try:
1020-
1021-
im.save(out, compression="tiff_adobe_deflate")
1022+
arguments = {"compression": "tiff_adobe_deflate"}
1023+
if argument:
1024+
arguments["strip_size"] = 2**18
1025+
im.save(out, **arguments)
10221026

10231027
with Image.open(out) as im:
10241028
assert len(im.tag_v2[STRIPOFFSETS]) == 1

Tests/test_file_pdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_monochrome(tmp_path):
4343

4444
# Act / Assert
4545
outfile = helper_save_as_pdf(tmp_path, mode)
46-
assert os.path.getsize(outfile) < 15000
46+
assert os.path.getsize(outfile) < 5000
4747

4848

4949
def test_greyscale(tmp_path):

src/PIL/PdfImagePlugin.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
##
2222

2323
import io
24+
import math
2425
import os
2526
import time
2627

@@ -123,8 +124,26 @@ def _save(im, fp, filename, save_all=False):
123124
params = None
124125
decode = None
125126

127+
#
128+
# Get image characteristics
129+
130+
width, height = im.size
131+
126132
if im.mode == "1":
127-
filter = "DCTDecode"
133+
filter = "CCITTFaxDecode"
134+
bits = 1
135+
params = PdfParser.PdfArray(
136+
[
137+
PdfParser.PdfDict(
138+
{
139+
"K": -1,
140+
"BlackIs1": True,
141+
"Columns": width,
142+
"Rows": height,
143+
}
144+
)
145+
]
146+
)
128147
colorspace = PdfParser.PdfName("DeviceGray")
129148
procset = "ImageB" # grayscale
130149
elif im.mode == "L":
@@ -161,6 +180,14 @@ def _save(im, fp, filename, save_all=False):
161180

162181
if filter == "ASCIIHexDecode":
163182
ImageFile._save(im, op, [("hex", (0, 0) + im.size, 0, im.mode)])
183+
elif filter == "CCITTFaxDecode":
184+
im.save(
185+
op,
186+
"TIFF",
187+
compression="group4",
188+
# use a single strip
189+
strip_size=math.ceil(im.width / 8) * im.height,
190+
)
164191
elif filter == "DCTDecode":
165192
Image.SAVE["JPEG"](im, op, filename)
166193
elif filter == "FlateDecode":
@@ -170,22 +197,24 @@ def _save(im, fp, filename, save_all=False):
170197
else:
171198
raise ValueError(f"unsupported PDF filter ({filter})")
172199

173-
#
174-
# Get image characteristics
175-
176-
width, height = im.size
200+
stream = op.getvalue()
201+
if filter == "CCITTFaxDecode":
202+
stream = stream[8:]
203+
filter = PdfParser.PdfArray([PdfParser.PdfName(filter)])
204+
else:
205+
filter = PdfParser.PdfName(filter)
177206

178207
existing_pdf.write_obj(
179208
image_refs[page_number],
180-
stream=op.getvalue(),
209+
stream=stream,
181210
Type=PdfParser.PdfName("XObject"),
182211
Subtype=PdfParser.PdfName("Image"),
183212
Width=width, # * 72.0 / resolution,
184213
Height=height, # * 72.0 / resolution,
185-
Filter=PdfParser.PdfName(filter),
214+
Filter=filter,
186215
BitsPerComponent=bits,
187216
Decode=decode,
188-
DecodeParams=params,
217+
DecodeParms=params,
189218
ColorSpace=colorspace,
190219
)
191220

src/PIL/TiffImagePlugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,8 @@ def _save(im, fp, filename):
16841684
stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8)
16851685
# aim for given strip size (64 KB by default) when using libtiff writer
16861686
if libtiff:
1687-
rows_per_strip = 1 if stride == 0 else min(STRIP_SIZE // stride, im.size[1])
1687+
im_strip_size = encoderinfo.get("strip_size", STRIP_SIZE)
1688+
rows_per_strip = 1 if stride == 0 else min(im_strip_size // stride, im.size[1])
16881689
# JPEG encoder expects multiple of 8 rows
16891690
if compression == "jpeg":
16901691
rows_per_strip = min(((rows_per_strip + 7) // 8) * 8, im.size[1])

0 commit comments

Comments
 (0)