Skip to content

Commit

Permalink
Restored filename in progress callback
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Oct 5, 2023
1 parent 3465a59 commit b5f3228
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 52 deletions.
62 changes: 52 additions & 10 deletions Tests/test_file_apng.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,11 +669,18 @@ def test_save_all_progress():
out = BytesIO()
progress = []

def callback(filename, frame_number, n_frames):
progress.append((filename, frame_number, n_frames))
def callback(state):
progress.append(state)

Image.new("RGB", (1, 1)).save(out, "PNG", save_all=True, progress=callback)
assert progress == [(0, 1, 1)]
assert progress == [
{
"image_index": 0,
"image_filename": None,
"completed_frames": 1,
"total_frames": 1,
}
]

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

assert progress == [
(0, 1, 7),
(1, 2, 7),
(2, 3, 7),
(2, 4, 7),
(2, 5, 7),
(2, 6, 7),
(2, 7, 7),
{
"image_index": 0,
"image_filename": "Tests/images/apng/single_frame.png",
"completed_frames": 1,
"total_frames": 7,
},
{
"image_index": 1,
"image_filename": "Tests/images/apng/single_frame.png",
"completed_frames": 2,
"total_frames": 7,
},
{
"image_index": 2,
"image_filename": "Tests/images/apng/delay.png",
"completed_frames": 3,
"total_frames": 7,
},
{
"image_index": 2,
"image_filename": "Tests/images/apng/delay.png",
"completed_frames": 4,
"total_frames": 7,
},
{
"image_index": 2,
"image_filename": "Tests/images/apng/delay.png",
"completed_frames": 5,
"total_frames": 7,
},
{
"image_index": 2,
"image_filename": "Tests/images/apng/delay.png",
"completed_frames": 6,
"total_frames": 7,
},
{
"image_index": 2,
"image_filename": "Tests/images/apng/delay.png",
"completed_frames": 7,
"total_frames": 7,
},
]


Expand Down
31 changes: 26 additions & 5 deletions Tests/test_file_gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,18 @@ def test_save_all_progress():
out = BytesIO()
progress = []

def callback(filename, frame_number, n_frames):
progress.append((filename, frame_number, n_frames))
def callback(state):
progress.append(state)

Image.new("RGB", (1, 1)).save(out, "GIF", save_all=True, progress=callback)
assert progress == [(0, 1, 1)]
assert progress == [
{
"image_index": 0,
"image_filename": None,
"completed_frames": 1,
"total_frames": 1,
}
]

out = BytesIO()
progress = []
Expand All @@ -282,9 +289,23 @@ 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 = [(0, 1, 32)]
expected = [
{
"image_index": 0,
"image_filename": "Tests/images/hopper.gif",
"completed_frames": 1,
"total_frames": 32,
}
]
for i in range(31):
expected.append((1, i + 2, 32))
expected.append(
{
"image_index": 1,
"image_filename": "Tests/images/chi.gif",
"completed_frames": i + 2,
"total_frames": 32,
}
)
assert progress == expected


Expand Down
41 changes: 34 additions & 7 deletions Tests/test_file_mpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,18 @@ def test_save_all_progress():
out = BytesIO()
progress = []

def callback(filename, frame_number, n_frames):
progress.append((filename, frame_number, n_frames))
def callback(state):
progress.append(state)

Image.new("RGB", (1, 1)).save(out, "MPO", save_all=True, progress=callback)
assert progress == [(0, 1, 1)]
assert progress == [
{
"image_index": 0,
"image_filename": None,
"completed_frames": 1,
"total_frames": 1,
}
]

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

assert progress == [
(0, 1, 4),
(0, 2, 4),
(1, 3, 4),
(1, 4, 4),
{
"image_index": 0,
"image_filename": "Tests/images/sugarshack.mpo",
"completed_frames": 1,
"total_frames": 4,
},
{
"image_index": 0,
"image_filename": "Tests/images/sugarshack.mpo",
"completed_frames": 2,
"total_frames": 4,
},
{
"image_index": 1,
"image_filename": "Tests/images/frozenpond.mpo",
"completed_frames": 3,
"total_frames": 4,
},
{
"image_index": 1,
"image_filename": "Tests/images/frozenpond.mpo",
"completed_frames": 4,
"total_frames": 4,
},
]
41 changes: 34 additions & 7 deletions Tests/test_file_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,18 @@ def test_save_all_progress():
out = BytesIO()
progress = []

def callback(filename, frame_number, n_frames):
progress.append((filename, frame_number, n_frames))
def callback(state):
progress.append(state)

Image.new("RGB", (1, 1)).save(out, "PDF", save_all=True, progress=callback)
assert progress == [(0, 1, 1)]
assert progress == [
{
"image_index": 0,
"image_filename": None,
"completed_frames": 1,
"total_frames": 1,
}
]

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

assert progress == [
(0, 1, 4),
(0, 2, 4),
(1, 3, 4),
(1, 4, 4),
{
"image_index": 0,
"image_filename": "Tests/images/sugarshack.mpo",
"completed_frames": 1,
"total_frames": 4,
},
{
"image_index": 0,
"image_filename": "Tests/images/sugarshack.mpo",
"completed_frames": 2,
"total_frames": 4,
},
{
"image_index": 1,
"image_filename": "Tests/images/frozenpond.mpo",
"completed_frames": 3,
"total_frames": 4,
},
{
"image_index": 1,
"image_filename": "Tests/images/frozenpond.mpo",
"completed_frames": 4,
"total_frames": 4,
},
]


Expand Down
41 changes: 34 additions & 7 deletions Tests/test_file_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,11 +692,18 @@ def test_save_all_progress(self):
out = BytesIO()
progress = []

def callback(filename, frame_number, n_frames):
progress.append((filename, frame_number, n_frames))
def callback(state):
progress.append(state)

Image.new("RGB", (1, 1)).save(out, "TIFF", save_all=True, progress=callback)
assert progress == [(0, 1, 1)]
assert progress == [
{
"image_index": 0,
"image_filename": None,
"completed_frames": 1,
"total_frames": 1,
}
]

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

assert progress == [
(0, 1, 4),
(1, 2, 4),
(1, 3, 4),
(1, 4, 4),
{
"image_index": 0,
"image_filename": "Tests/images/hopper.tif",
"completed_frames": 1,
"total_frames": 4,
},
{
"image_index": 1,
"image_filename": "Tests/images/multipage.tiff",
"completed_frames": 2,
"total_frames": 4,
},
{
"image_index": 1,
"image_filename": "Tests/images/multipage.tiff",
"completed_frames": 3,
"total_frames": 4,
},
{
"image_index": 1,
"image_filename": "Tests/images/multipage.tiff",
"completed_frames": 4,
"total_frames": 4,
},
]

def test_saving_icc_profile(self, tmp_path):
Expand Down
31 changes: 26 additions & 5 deletions Tests/test_file_webp.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,18 @@ def test_save_all_progress(self):
out = BytesIO()
progress = []

def callback(filename, frame_number, n_frames):
progress.append((filename, frame_number, n_frames))
def callback(state):
progress.append(state)

Image.new("RGB", (1, 1)).save(out, "WEBP", save_all=True, progress=callback)
assert progress == [(0, 1, 1)]
assert progress == [
{
"image_index": 0,
"image_filename": None,
"completed_frames": 1,
"total_frames": 1,
}
]

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

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

def test_icc_profile(self, tmp_path):
Expand Down
18 changes: 16 additions & 2 deletions src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,14 @@ def _write_multiple_frames(im, fp, palette):
if encoderinfo.get("duration"):
previous["encoderinfo"]["duration"] += encoderinfo["duration"]
if progress:
progress(i, frame_count, n_frames)
progress(
{
"image_index": i,
"image_filename": getattr(imSequence, "filename", None),
"completed_frames": frame_count,
"total_frames": n_frames,
}
)
continue
if encoderinfo.get("disposal") == 2:
if background_im is None:
Expand All @@ -633,7 +640,14 @@ def _write_multiple_frames(im, fp, palette):
bbox = None
im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo})
if progress:
progress(i, frame_count, n_frames)
progress(
{
"image_index": i,
"image_filename": getattr(imSequence, "filename", None),
"completed_frames": frame_count,
"total_frames": n_frames,
}
)

if len(im_frames) > 1:
for frame_data in im_frames:
Expand Down
Loading

0 comments on commit b5f3228

Please sign in to comment.