Skip to content

Commit

Permalink
Pass back index of image in sequence, instead of filename
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Oct 2, 2023
1 parent 6850465 commit 3465a59
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 52 deletions.
16 changes: 8 additions & 8 deletions Tests/test_file_apng.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ def callback(filename, frame_number, n_frames):
progress.append((filename, frame_number, n_frames))

Image.new("RGB", (1, 1)).save(out, "PNG", save_all=True, progress=callback)
assert progress == [(None, 1, 1)]
assert progress == [(0, 1, 1)]

out = BytesIO()
progress = []
Expand All @@ -685,13 +685,13 @@ def callback(filename, frame_number, n_frames):
)

assert progress == [
("Tests/images/apng/single_frame.png", 1, 7),
("Tests/images/apng/single_frame.png", 2, 7),
("Tests/images/apng/delay.png", 3, 7),
("Tests/images/apng/delay.png", 4, 7),
("Tests/images/apng/delay.png", 5, 7),
("Tests/images/apng/delay.png", 6, 7),
("Tests/images/apng/delay.png", 7, 7),
(0, 1, 7),
(1, 2, 7),
(2, 3, 7),
(2, 4, 7),
(2, 5, 7),
(2, 6, 7),
(2, 7, 7),
]


Expand Down
6 changes: 3 additions & 3 deletions Tests/test_file_gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def callback(filename, frame_number, n_frames):
progress.append((filename, frame_number, n_frames))

Image.new("RGB", (1, 1)).save(out, "GIF", save_all=True, progress=callback)
assert progress == [(None, 1, 1)]
assert progress == [(0, 1, 1)]

out = BytesIO()
progress = []
Expand All @@ -282,9 +282,9 @@ def callback(filename, frame_number, n_frames):
with Image.open("Tests/images/chi.gif") as im2:
im.save(out, "GIF", save_all=True, append_images=[im2], progress=callback)

expected = [("Tests/images/hopper.gif", 1, 32)]
expected = [(0, 1, 32)]
for i in range(31):
expected.append(("Tests/images/chi.gif", i + 2, 32))
expected.append((1, i + 2, 32))
assert progress == expected


Expand Down
10 changes: 5 additions & 5 deletions Tests/test_file_mpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def callback(filename, frame_number, n_frames):
progress.append((filename, frame_number, n_frames))

Image.new("RGB", (1, 1)).save(out, "MPO", save_all=True, progress=callback)
assert progress == [(None, 1, 1)]
assert progress == [(0, 1, 1)]

out = BytesIO()
progress = []
Expand All @@ -298,8 +298,8 @@ def callback(filename, frame_number, n_frames):
im.save(out, "MPO", save_all=True, append_images=[im2], progress=callback)

assert progress == [
("Tests/images/sugarshack.mpo", 1, 4),
("Tests/images/sugarshack.mpo", 2, 4),
("Tests/images/frozenpond.mpo", 3, 4),
("Tests/images/frozenpond.mpo", 4, 4),
(0, 1, 4),
(0, 2, 4),
(1, 3, 4),
(1, 4, 4),
]
10 changes: 5 additions & 5 deletions Tests/test_file_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def callback(filename, frame_number, n_frames):
progress.append((filename, frame_number, n_frames))

Image.new("RGB", (1, 1)).save(out, "PDF", save_all=True, progress=callback)
assert progress == [(None, 1, 1)]
assert progress == [(0, 1, 1)]

out = BytesIO()
progress = []
Expand All @@ -187,10 +187,10 @@ def callback(filename, frame_number, n_frames):
im.save(out, "PDF", save_all=True, append_images=[im2], progress=callback)

assert progress == [
("Tests/images/sugarshack.mpo", 1, 4),
("Tests/images/sugarshack.mpo", 2, 4),
("Tests/images/frozenpond.mpo", 3, 4),
("Tests/images/frozenpond.mpo", 4, 4),
(0, 1, 4),
(0, 2, 4),
(1, 3, 4),
(1, 4, 4),
]


Expand Down
10 changes: 5 additions & 5 deletions Tests/test_file_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ def callback(filename, frame_number, n_frames):
progress.append((filename, frame_number, n_frames))

Image.new("RGB", (1, 1)).save(out, "TIFF", save_all=True, progress=callback)
assert progress == [(None, 1, 1)]
assert progress == [(0, 1, 1)]

out = BytesIO()
progress = []
Expand All @@ -708,10 +708,10 @@ def callback(filename, frame_number, n_frames):
)

assert progress == [
("Tests/images/hopper.tif", 1, 4),
("Tests/images/multipage.tiff", 2, 4),
("Tests/images/multipage.tiff", 3, 4),
("Tests/images/multipage.tiff", 4, 4),
(0, 1, 4),
(1, 2, 4),
(1, 3, 4),
(1, 4, 4),
]

def test_saving_icc_profile(self, tmp_path):
Expand Down
6 changes: 3 additions & 3 deletions Tests/test_file_webp.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def callback(filename, frame_number, n_frames):
progress.append((filename, frame_number, n_frames))

Image.new("RGB", (1, 1)).save(out, "WEBP", save_all=True, progress=callback)
assert progress == [(None, 1, 1)]
assert progress == [(0, 1, 1)]

out = BytesIO()
progress = []
Expand All @@ -142,8 +142,8 @@ def callback(filename, frame_number, n_frames):

expected = []
for i in range(42):
expected.append(("Tests/images/iss634.webp", i + 1, 43))
expected.append((None, 43, 43))
expected.append((0, i + 1, 43))
expected.append((1, 43, 43))
assert progress == expected

def test_icc_profile(self, tmp_path):
Expand Down
8 changes: 3 additions & 5 deletions src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def _write_multiple_frames(im, fp, palette):
im_frames = []
frame_count = 0
background_im = None
for imSequence in imSequences:
for i, imSequence in enumerate(imSequences):
for im_frame in ImageSequence.Iterator(imSequence):
# a copy is required here since seek can still mutate the image
im_frame = _normalize_mode(im_frame.copy())
Expand Down Expand Up @@ -618,9 +618,7 @@ def _write_multiple_frames(im, fp, palette):
if encoderinfo.get("duration"):
previous["encoderinfo"]["duration"] += encoderinfo["duration"]
if progress:
progress(
getattr(imSequence, "filename", None), frame_count, n_frames
)
progress(i, frame_count, n_frames)
continue
if encoderinfo.get("disposal") == 2:
if background_im is None:
Expand All @@ -635,7 +633,7 @@ def _write_multiple_frames(im, fp, palette):
bbox = None
im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo})
if progress:
progress(getattr(imSequence, "filename", None), frame_count, n_frames)
progress(i, frame_count, n_frames)

if len(im_frames) > 1:
for frame_data in im_frames:
Expand Down
6 changes: 3 additions & 3 deletions src/PIL/MpoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _save_all(im, fp, filename):
if not animated:
_save(im, fp, filename)
if progress:
progress(getattr(im, "filename", None), 1, 1)
progress(0, 1, 1)
return

mpf_offset = 28
Expand All @@ -62,7 +62,7 @@ def _save_all(im, fp, filename):
n_frames = 0
for imSequence in imSequences:
n_frames += getattr(imSequence, "n_frames", 1)
for imSequence in imSequences:
for i, imSequence in enumerate(imSequences):
for im_frame in ImageSequence.Iterator(imSequence):
if not offsets:
# APP2 marker
Expand All @@ -83,7 +83,7 @@ def _save_all(im, fp, filename):
offsets.append(fp.tell() - offsets[-1])
if progress:
frame_number += 1
progress(getattr(imSequence, "filename", None), frame_number, n_frames)
progress(i, frame_number, n_frames)

ifd = TiffImagePlugin.ImageFileDirectory_v2()
ifd[0xB000] = b"0100"
Expand Down
6 changes: 2 additions & 4 deletions src/PIL/PdfImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def _save(im, fp, filename, save_all=False):

progress = im.encoderinfo.get("progress")
page_number = 0
for im_sequence in ims:
for i, im_sequence in enumerate(ims):
im_pages = 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)
Expand Down Expand Up @@ -283,9 +283,7 @@ def _save(im, fp, filename, save_all=False):

page_number += 1
if progress:
progress(
getattr(im_sequence, "filename", None), page_number, number_of_pages
)
progress(i, page_number, number_of_pages)

#
# trailer
Expand Down
8 changes: 3 additions & 5 deletions src/PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images)

im_frames = []
frame_count = 0
for imSequence in imSequences:
for i, imSequence in enumerate(imSequences):
for im_frame in ImageSequence.Iterator(imSequence):
if im_frame.mode == rawmode:
im_frame = im_frame.copy()
Expand Down Expand Up @@ -1155,17 +1155,15 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images)
"duration", duration
)
if progress:
progress(
getattr(imSequence, "filename", None), frame_count, n_frames
)
progress(i, frame_count, n_frames)
continue
else:
bbox = None
if "duration" not in encoderinfo:
encoderinfo["duration"] = duration
im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo})
if progress:
progress(getattr(imSequence, "filename", None), frame_count, n_frames)
progress(i, frame_count, n_frames)

# animation control
chunk(
Expand Down
6 changes: 3 additions & 3 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2122,7 +2122,7 @@ def _save_all(im, fp, filename):
if not hasattr(im, "n_frames") and not append_images:
_save(im, fp, filename)
if progress:
progress(getattr(im, "filename", None), 1, 1)
progress(0, 1, 1)
return

cur_idx = im.tell()
Expand All @@ -2134,7 +2134,7 @@ def _save_all(im, fp, filename):
n_frames += getattr(ims, "n_frames", 1)
try:
with AppendingTiffWriter(fp) as tf:
for ims in imSequences:
for i, ims in enumerate(imSequences):
ims.encoderinfo = encoderinfo
ims.encoderconfig = encoderconfig
if not hasattr(ims, "n_frames"):
Expand All @@ -2148,7 +2148,7 @@ def _save_all(im, fp, filename):
_save(ims, tf, filename)
if progress:
frame_number += 1
progress(getattr(ims, "filename", None), frame_number, n_frames)
progress(i, frame_number, n_frames)

tf.newFrame()
finally:
Expand Down
6 changes: 3 additions & 3 deletions src/PIL/WebPImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def _save_all(im, fp, filename):
if total == 1:
_save(im, fp, filename)
if progress:
progress(getattr(im, "filename", None), 1, 1)
progress(0, 1, 1)
return

background = (0, 0, 0, 0)
Expand Down Expand Up @@ -261,7 +261,7 @@ def _save_all(im, fp, filename):
timestamp = 0
cur_idx = im.tell()
try:
for ims in [im] + append_images:
for i, ims in enumerate([im] + append_images):
# Get # of frames in this image
nfr = getattr(ims, "n_frames", 1)

Expand Down Expand Up @@ -304,7 +304,7 @@ def _save_all(im, fp, filename):
timestamp += duration
frame_idx += 1
if progress:
progress(getattr(ims, "filename", None), frame_idx, total)
progress(i, frame_idx, total)

finally:
im.seek(cur_idx)
Expand Down

0 comments on commit 3465a59

Please sign in to comment.