mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-10 16:22:22 +03:00
Pass back index of image in sequence, instead of filename
This commit is contained in:
parent
6850465d54
commit
3465a598e2
|
@ -673,7 +673,7 @@ def test_save_all_progress():
|
||||||
progress.append((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)
|
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()
|
out = BytesIO()
|
||||||
progress = []
|
progress = []
|
||||||
|
@ -685,13 +685,13 @@ def test_save_all_progress():
|
||||||
)
|
)
|
||||||
|
|
||||||
assert progress == [
|
assert progress == [
|
||||||
("Tests/images/apng/single_frame.png", 1, 7),
|
(0, 1, 7),
|
||||||
("Tests/images/apng/single_frame.png", 2, 7),
|
(1, 2, 7),
|
||||||
("Tests/images/apng/delay.png", 3, 7),
|
(2, 3, 7),
|
||||||
("Tests/images/apng/delay.png", 4, 7),
|
(2, 4, 7),
|
||||||
("Tests/images/apng/delay.png", 5, 7),
|
(2, 5, 7),
|
||||||
("Tests/images/apng/delay.png", 6, 7),
|
(2, 6, 7),
|
||||||
("Tests/images/apng/delay.png", 7, 7),
|
(2, 7, 7),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -273,7 +273,7 @@ def test_save_all_progress():
|
||||||
progress.append((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)
|
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()
|
out = BytesIO()
|
||||||
progress = []
|
progress = []
|
||||||
|
@ -282,9 +282,9 @@ def test_save_all_progress():
|
||||||
with Image.open("Tests/images/chi.gif") as im2:
|
with Image.open("Tests/images/chi.gif") as im2:
|
||||||
im.save(out, "GIF", save_all=True, append_images=[im2], progress=callback)
|
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):
|
for i in range(31):
|
||||||
expected.append(("Tests/images/chi.gif", i + 2, 32))
|
expected.append((1, i + 2, 32))
|
||||||
assert progress == expected
|
assert progress == expected
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -288,7 +288,7 @@ def test_save_all_progress():
|
||||||
progress.append((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)
|
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()
|
out = BytesIO()
|
||||||
progress = []
|
progress = []
|
||||||
|
@ -298,8 +298,8 @@ def test_save_all_progress():
|
||||||
im.save(out, "MPO", save_all=True, append_images=[im2], progress=callback)
|
im.save(out, "MPO", save_all=True, append_images=[im2], progress=callback)
|
||||||
|
|
||||||
assert progress == [
|
assert progress == [
|
||||||
("Tests/images/sugarshack.mpo", 1, 4),
|
(0, 1, 4),
|
||||||
("Tests/images/sugarshack.mpo", 2, 4),
|
(0, 2, 4),
|
||||||
("Tests/images/frozenpond.mpo", 3, 4),
|
(1, 3, 4),
|
||||||
("Tests/images/frozenpond.mpo", 4, 4),
|
(1, 4, 4),
|
||||||
]
|
]
|
||||||
|
|
|
@ -177,7 +177,7 @@ def test_save_all_progress():
|
||||||
progress.append((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)
|
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()
|
out = BytesIO()
|
||||||
progress = []
|
progress = []
|
||||||
|
@ -187,10 +187,10 @@ def test_save_all_progress():
|
||||||
im.save(out, "PDF", save_all=True, append_images=[im2], progress=callback)
|
im.save(out, "PDF", save_all=True, append_images=[im2], progress=callback)
|
||||||
|
|
||||||
assert progress == [
|
assert progress == [
|
||||||
("Tests/images/sugarshack.mpo", 1, 4),
|
(0, 1, 4),
|
||||||
("Tests/images/sugarshack.mpo", 2, 4),
|
(0, 2, 4),
|
||||||
("Tests/images/frozenpond.mpo", 3, 4),
|
(1, 3, 4),
|
||||||
("Tests/images/frozenpond.mpo", 4, 4),
|
(1, 4, 4),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -696,7 +696,7 @@ class TestFileTiff:
|
||||||
progress.append((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)
|
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()
|
out = BytesIO()
|
||||||
progress = []
|
progress = []
|
||||||
|
@ -708,10 +708,10 @@ class TestFileTiff:
|
||||||
)
|
)
|
||||||
|
|
||||||
assert progress == [
|
assert progress == [
|
||||||
("Tests/images/hopper.tif", 1, 4),
|
(0, 1, 4),
|
||||||
("Tests/images/multipage.tiff", 2, 4),
|
(1, 2, 4),
|
||||||
("Tests/images/multipage.tiff", 3, 4),
|
(1, 3, 4),
|
||||||
("Tests/images/multipage.tiff", 4, 4),
|
(1, 4, 4),
|
||||||
]
|
]
|
||||||
|
|
||||||
def test_saving_icc_profile(self, tmp_path):
|
def test_saving_icc_profile(self, tmp_path):
|
||||||
|
|
|
@ -131,7 +131,7 @@ class TestFileWebp:
|
||||||
progress.append((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)
|
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()
|
out = BytesIO()
|
||||||
progress = []
|
progress = []
|
||||||
|
@ -142,8 +142,8 @@ class TestFileWebp:
|
||||||
|
|
||||||
expected = []
|
expected = []
|
||||||
for i in range(42):
|
for i in range(42):
|
||||||
expected.append(("Tests/images/iss634.webp", i + 1, 43))
|
expected.append((0, i + 1, 43))
|
||||||
expected.append((None, 43, 43))
|
expected.append((1, 43, 43))
|
||||||
assert progress == expected
|
assert progress == expected
|
||||||
|
|
||||||
def test_icc_profile(self, tmp_path):
|
def test_icc_profile(self, tmp_path):
|
||||||
|
|
|
@ -587,7 +587,7 @@ def _write_multiple_frames(im, fp, palette):
|
||||||
im_frames = []
|
im_frames = []
|
||||||
frame_count = 0
|
frame_count = 0
|
||||||
background_im = None
|
background_im = None
|
||||||
for imSequence in imSequences:
|
for i, imSequence in enumerate(imSequences):
|
||||||
for im_frame in ImageSequence.Iterator(imSequence):
|
for im_frame in ImageSequence.Iterator(imSequence):
|
||||||
# a copy is required here since seek can still mutate the image
|
# a copy is required here since seek can still mutate the image
|
||||||
im_frame = _normalize_mode(im_frame.copy())
|
im_frame = _normalize_mode(im_frame.copy())
|
||||||
|
@ -618,9 +618,7 @@ def _write_multiple_frames(im, fp, palette):
|
||||||
if encoderinfo.get("duration"):
|
if encoderinfo.get("duration"):
|
||||||
previous["encoderinfo"]["duration"] += encoderinfo["duration"]
|
previous["encoderinfo"]["duration"] += encoderinfo["duration"]
|
||||||
if progress:
|
if progress:
|
||||||
progress(
|
progress(i, frame_count, n_frames)
|
||||||
getattr(imSequence, "filename", None), frame_count, n_frames
|
|
||||||
)
|
|
||||||
continue
|
continue
|
||||||
if encoderinfo.get("disposal") == 2:
|
if encoderinfo.get("disposal") == 2:
|
||||||
if background_im is None:
|
if background_im is None:
|
||||||
|
@ -635,7 +633,7 @@ def _write_multiple_frames(im, fp, palette):
|
||||||
bbox = None
|
bbox = None
|
||||||
im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo})
|
im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo})
|
||||||
if progress:
|
if progress:
|
||||||
progress(getattr(imSequence, "filename", None), frame_count, n_frames)
|
progress(i, frame_count, n_frames)
|
||||||
|
|
||||||
if len(im_frames) > 1:
|
if len(im_frames) > 1:
|
||||||
for frame_data in im_frames:
|
for frame_data in im_frames:
|
||||||
|
|
|
@ -51,7 +51,7 @@ def _save_all(im, fp, filename):
|
||||||
if not animated:
|
if not animated:
|
||||||
_save(im, fp, filename)
|
_save(im, fp, filename)
|
||||||
if progress:
|
if progress:
|
||||||
progress(getattr(im, "filename", None), 1, 1)
|
progress(0, 1, 1)
|
||||||
return
|
return
|
||||||
|
|
||||||
mpf_offset = 28
|
mpf_offset = 28
|
||||||
|
@ -62,7 +62,7 @@ def _save_all(im, fp, filename):
|
||||||
n_frames = 0
|
n_frames = 0
|
||||||
for imSequence in imSequences:
|
for imSequence in imSequences:
|
||||||
n_frames += getattr(imSequence, "n_frames", 1)
|
n_frames += getattr(imSequence, "n_frames", 1)
|
||||||
for imSequence in imSequences:
|
for i, imSequence in enumerate(imSequences):
|
||||||
for im_frame in ImageSequence.Iterator(imSequence):
|
for im_frame in ImageSequence.Iterator(imSequence):
|
||||||
if not offsets:
|
if not offsets:
|
||||||
# APP2 marker
|
# APP2 marker
|
||||||
|
@ -83,7 +83,7 @@ def _save_all(im, fp, filename):
|
||||||
offsets.append(fp.tell() - offsets[-1])
|
offsets.append(fp.tell() - offsets[-1])
|
||||||
if progress:
|
if progress:
|
||||||
frame_number += 1
|
frame_number += 1
|
||||||
progress(getattr(imSequence, "filename", None), frame_number, n_frames)
|
progress(i, frame_number, n_frames)
|
||||||
|
|
||||||
ifd = TiffImagePlugin.ImageFileDirectory_v2()
|
ifd = TiffImagePlugin.ImageFileDirectory_v2()
|
||||||
ifd[0xB000] = b"0100"
|
ifd[0xB000] = b"0100"
|
||||||
|
|
|
@ -248,7 +248,7 @@ def _save(im, fp, filename, save_all=False):
|
||||||
|
|
||||||
progress = im.encoderinfo.get("progress")
|
progress = im.encoderinfo.get("progress")
|
||||||
page_number = 0
|
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]
|
im_pages = ImageSequence.Iterator(im_sequence) if save_all else [im_sequence]
|
||||||
for im in im_pages:
|
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, image_refs)
|
||||||
|
@ -283,9 +283,7 @@ def _save(im, fp, filename, save_all=False):
|
||||||
|
|
||||||
page_number += 1
|
page_number += 1
|
||||||
if progress:
|
if progress:
|
||||||
progress(
|
progress(i, page_number, number_of_pages)
|
||||||
getattr(im_sequence, "filename", None), page_number, number_of_pages
|
|
||||||
)
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# trailer
|
# trailer
|
||||||
|
|
|
@ -1104,7 +1104,7 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images)
|
||||||
|
|
||||||
im_frames = []
|
im_frames = []
|
||||||
frame_count = 0
|
frame_count = 0
|
||||||
for imSequence in imSequences:
|
for i, imSequence in enumerate(imSequences):
|
||||||
for im_frame in ImageSequence.Iterator(imSequence):
|
for im_frame in ImageSequence.Iterator(imSequence):
|
||||||
if im_frame.mode == rawmode:
|
if im_frame.mode == rawmode:
|
||||||
im_frame = im_frame.copy()
|
im_frame = im_frame.copy()
|
||||||
|
@ -1155,9 +1155,7 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images)
|
||||||
"duration", duration
|
"duration", duration
|
||||||
)
|
)
|
||||||
if progress:
|
if progress:
|
||||||
progress(
|
progress(i, frame_count, n_frames)
|
||||||
getattr(imSequence, "filename", None), frame_count, n_frames
|
|
||||||
)
|
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
bbox = None
|
bbox = None
|
||||||
|
@ -1165,7 +1163,7 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images)
|
||||||
encoderinfo["duration"] = duration
|
encoderinfo["duration"] = duration
|
||||||
im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo})
|
im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo})
|
||||||
if progress:
|
if progress:
|
||||||
progress(getattr(imSequence, "filename", None), frame_count, n_frames)
|
progress(i, frame_count, n_frames)
|
||||||
|
|
||||||
# animation control
|
# animation control
|
||||||
chunk(
|
chunk(
|
||||||
|
|
|
@ -2122,7 +2122,7 @@ def _save_all(im, fp, filename):
|
||||||
if not hasattr(im, "n_frames") and not append_images:
|
if not hasattr(im, "n_frames") and not append_images:
|
||||||
_save(im, fp, filename)
|
_save(im, fp, filename)
|
||||||
if progress:
|
if progress:
|
||||||
progress(getattr(im, "filename", None), 1, 1)
|
progress(0, 1, 1)
|
||||||
return
|
return
|
||||||
|
|
||||||
cur_idx = im.tell()
|
cur_idx = im.tell()
|
||||||
|
@ -2134,7 +2134,7 @@ def _save_all(im, fp, filename):
|
||||||
n_frames += getattr(ims, "n_frames", 1)
|
n_frames += getattr(ims, "n_frames", 1)
|
||||||
try:
|
try:
|
||||||
with AppendingTiffWriter(fp) as tf:
|
with AppendingTiffWriter(fp) as tf:
|
||||||
for ims in imSequences:
|
for i, ims in enumerate(imSequences):
|
||||||
ims.encoderinfo = encoderinfo
|
ims.encoderinfo = encoderinfo
|
||||||
ims.encoderconfig = encoderconfig
|
ims.encoderconfig = encoderconfig
|
||||||
if not hasattr(ims, "n_frames"):
|
if not hasattr(ims, "n_frames"):
|
||||||
|
@ -2148,7 +2148,7 @@ def _save_all(im, fp, filename):
|
||||||
_save(ims, tf, filename)
|
_save(ims, tf, filename)
|
||||||
if progress:
|
if progress:
|
||||||
frame_number += 1
|
frame_number += 1
|
||||||
progress(getattr(ims, "filename", None), frame_number, n_frames)
|
progress(i, frame_number, n_frames)
|
||||||
|
|
||||||
tf.newFrame()
|
tf.newFrame()
|
||||||
finally:
|
finally:
|
||||||
|
|
|
@ -188,7 +188,7 @@ def _save_all(im, fp, filename):
|
||||||
if total == 1:
|
if total == 1:
|
||||||
_save(im, fp, filename)
|
_save(im, fp, filename)
|
||||||
if progress:
|
if progress:
|
||||||
progress(getattr(im, "filename", None), 1, 1)
|
progress(0, 1, 1)
|
||||||
return
|
return
|
||||||
|
|
||||||
background = (0, 0, 0, 0)
|
background = (0, 0, 0, 0)
|
||||||
|
@ -261,7 +261,7 @@ def _save_all(im, fp, filename):
|
||||||
timestamp = 0
|
timestamp = 0
|
||||||
cur_idx = im.tell()
|
cur_idx = im.tell()
|
||||||
try:
|
try:
|
||||||
for ims in [im] + append_images:
|
for i, ims in enumerate([im] + append_images):
|
||||||
# Get # of frames in this image
|
# Get # of frames in this image
|
||||||
nfr = getattr(ims, "n_frames", 1)
|
nfr = getattr(ims, "n_frames", 1)
|
||||||
|
|
||||||
|
@ -304,7 +304,7 @@ def _save_all(im, fp, filename):
|
||||||
timestamp += duration
|
timestamp += duration
|
||||||
frame_idx += 1
|
frame_idx += 1
|
||||||
if progress:
|
if progress:
|
||||||
progress(getattr(ims, "filename", None), frame_idx, total)
|
progress(i, frame_idx, total)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
im.seek(cur_idx)
|
im.seek(cur_idx)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user