Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Tests/test_file_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ def im_generator(ims: list[Image.Image]) -> Generator[Image.Image]:
assert os.path.getsize(outfile) > 0


def test_save_all_object_ids(tmp_path: Path) -> None:
outfile = str(tmp_path / "temp.pdf")
with Image.open("Tests/images/transparent_dispose.gif") as im:
im.save(outfile, save_all=True)

with PdfParser.PdfParser(outfile) as pdf:
# Test that each object id was used
for object_id in pdf.xref_table.existing_entries:
pdf.read_indirect(PdfParser.IndirectReference(object_id, 0))


def test_multiframe_normal_save(tmp_path: Path) -> None:
# Test saving a multiframe image without save_all
with Image.open("Tests/images/dispose_bgnd.gif") as im:
Expand Down
15 changes: 4 additions & 11 deletions src/PIL/PdfImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def _write_image(
im: Image.Image,
filename: str | bytes,
existing_pdf: PdfParser.PdfParser,
image_refs: list[PdfParser.IndirectReference],
) -> tuple[PdfParser.IndirectReference, str]:
# FIXME: Should replace ASCIIHexDecode with RunLengthDecode
# (packbits) or LZWDecode (tiff/lzw compression). Note that
Expand Down Expand Up @@ -118,7 +117,7 @@ def _write_image(
smask = im.convert("LA").getchannel("A")
smask.encoderinfo = {}

image_ref = _write_image(smask, filename, existing_pdf, image_refs)[0]
image_ref = _write_image(smask, filename, existing_pdf)[0]
dict_obj["SMask"] = image_ref
elif im.mode == "RGB":
decode_filter = "DCTDecode"
Expand Down Expand Up @@ -173,9 +172,8 @@ def _write_image(
else:
filter = PdfParser.PdfName(decode_filter)

image_ref = image_refs.pop(0)
existing_pdf.write_obj(
image_ref,
image_ref = existing_pdf.write_obj(
None,
stream=stream,
Type=PdfParser.PdfName("XObject"),
Subtype=PdfParser.PdfName("Image"),
Expand Down Expand Up @@ -241,7 +239,6 @@ def _save(
append_im.encoderinfo = im.encoderinfo.copy()
ims.append(append_im)
number_of_pages = 0
image_refs = []
page_refs = []
contents_refs = []
for im in ims:
Expand All @@ -250,10 +247,6 @@ def _save(
im_number_of_pages = getattr(im, "n_frames", 1)
number_of_pages += im_number_of_pages
for i in range(im_number_of_pages):
image_refs.append(existing_pdf.next_object_id(0))
if im.mode == "P" and "transparency" in im.info:
image_refs.append(existing_pdf.next_object_id(0))

page_refs.append(existing_pdf.next_object_id(0))
contents_refs.append(existing_pdf.next_object_id(0))
existing_pdf.pages.append(page_refs[-1])
Expand All @@ -268,7 +261,7 @@ def _save(
ImageSequence.Iterator(im_sequence) if save_all else [im_sequence]
)
for im in im_pages:
image_ref, procset = _write_image(im, filename, existing_pdf, image_refs)
image_ref, procset = _write_image(im, filename, existing_pdf)

#
# page
Expand Down
Loading