Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added progress callback when save_all is used #7435

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
53 changes: 53 additions & 0 deletions Tests/test_file_apng.py
@@ -1,5 +1,6 @@
from __future__ import annotations

from io import BytesIO
from pathlib import Path

import pytest
Expand Down Expand Up @@ -668,6 +669,58 @@ def test_apng_save_blend(tmp_path: Path) -> None:
assert im.getpixel((0, 0)) == (0, 255, 0, 255)


def test_save_all_progress() -> None:
out = BytesIO()
progress = []

def callback(state):
if state["image_filename"]:
state["image_filename"] = (
state["image_filename"].replace("\\", "/").split("Tests/images/")[-1]
)
progress.append(state)

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

out = BytesIO()
progress = []

with Image.open("Tests/images/apng/single_frame.png") as im:
with Image.open("Tests/images/apng/delay.png") as im2:
im.save(
out, "PNG", save_all=True, append_images=[im, im2], progress=callback
)

expected = []
for i in range(2):
expected.append(
{
"image_index": i,
"image_filename": "apng/single_frame.png",
"completed_frames": i + 1,
"total_frames": 7,
}
)
for i in range(5):
expected.append(
{
"image_index": 2,
"image_filename": "apng/delay.png",
"completed_frames": i + 3,
"total_frames": 7,
}
)
assert progress == expected


def test_apng_save_size(tmp_path: Path) -> None:
test_file = str(tmp_path / "temp.png")

Expand Down
48 changes: 48 additions & 0 deletions Tests/test_file_gif.py
Expand Up @@ -292,6 +292,54 @@ def test_roundtrip_save_all_1(tmp_path: Path) -> None:
assert reloaded.getpixel((0, 0)) == 255


def test_save_all_progress():
out = BytesIO()
progress = []

def callback(state):
if state["image_filename"]:
state["image_filename"] = (
state["image_filename"].replace("\\", "/").split("Tests/images/")[-1]
)
progress.append(state)

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

out = BytesIO()
progress = []

with Image.open("Tests/images/chi.gif") as im2:
im = Image.new("RGB", im2.size)
im.save(out, "GIF", save_all=True, append_images=[im2], progress=callback)

expected = [
{
"image_index": 0,
"image_filename": None,
"completed_frames": 1,
"total_frames": 32,
}
]
for i in range(31):
expected.append(
{
"image_index": 1,
"image_filename": "chi.gif",
"completed_frames": i + 2,
"total_frames": 32,
}
)
assert progress == expected


@pytest.mark.parametrize(
"path, mode",
(
Expand Down
42 changes: 42 additions & 0 deletions Tests/test_file_mpo.py
Expand Up @@ -278,3 +278,45 @@ def test_save_all() -> None:
# Test that a single frame image will not be saved as an MPO
jpg = roundtrip(im, save_all=True)
assert "mp" not in jpg.info


def test_save_all_progress():
out = BytesIO()
progress = []

def callback(state):
if state["image_filename"]:
state["image_filename"] = (
state["image_filename"].replace("\\", "/").split("Tests/images/")[-1]
)
progress.append(state)

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

out = BytesIO()
progress = []

with Image.open("Tests/images/sugarshack.mpo") as im:
with Image.open("Tests/images/frozenpond.mpo") as im2:
im.save(out, "MPO", save_all=True, append_images=[im2], progress=callback)

expected = []
for i, filename in enumerate(["sugarshack.mpo", "frozenpond.mpo"]):
for j in range(2):
expected.append(
{
"image_index": i,
"image_filename": filename,
"completed_frames": i * 2 + j + 1,
"total_frames": 4,
}
)
assert progress == expected
48 changes: 45 additions & 3 deletions Tests/test_file_pdf.py
@@ -1,10 +1,10 @@
from __future__ import annotations

import io
import os
import os.path
import tempfile
import time
from io import BytesIO
from pathlib import Path
from typing import Any, Generator

Expand Down Expand Up @@ -173,6 +173,48 @@ def im_generator(ims: list[Image.Image]) -> Generator[Image.Image, None, None]:
assert os.path.getsize(outfile) > 0


def test_save_all_progress() -> None:
out = BytesIO()
progress = []

def callback(state):
if state["image_filename"]:
state["image_filename"] = (
state["image_filename"].replace("\\", "/").split("Tests/images/")[-1]
)
progress.append(state)

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

out = BytesIO()
progress = []

with Image.open("Tests/images/sugarshack.mpo") as im:
with Image.open("Tests/images/frozenpond.mpo") as im2:
im.save(out, "PDF", save_all=True, append_images=[im2], progress=callback)

expected = []
for i, filename in enumerate(["sugarshack.mpo", "frozenpond.mpo"]):
for j in range(2):
expected.append(
{
"image_index": i,
"image_filename": filename,
"completed_frames": i * 2 + j + 1,
"total_frames": 4,
}
)
assert progress == expected


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 Expand Up @@ -327,12 +369,12 @@ def test_pdf_info(tmp_path: Path) -> None:

def test_pdf_append_to_bytesio() -> None:
im = hopper("RGB")
f = io.BytesIO()
f = BytesIO()
im.save(f, format="PDF")
initial_size = len(f.getvalue())
assert initial_size > 0
im = hopper("P")
f = io.BytesIO(f.getvalue())
f = BytesIO(f.getvalue())
im.save(f, format="PDF", append=True)
assert len(f.getvalue()) > initial_size

Expand Down
53 changes: 52 additions & 1 deletion Tests/test_file_tiff.py
Expand Up @@ -677,7 +677,7 @@ def test_palette(self, mode: str, tmp_path: Path) -> None:
with Image.open(outfile) as reloaded:
assert_image_equal(im.convert("RGB"), reloaded.convert("RGB"))

def test_tiff_save_all(self) -> None:
def test_save_all(self) -> None:
mp = BytesIO()
with Image.open("Tests/images/multipage.tiff") as im:
im.save(mp, format="tiff", save_all=True)
Expand Down Expand Up @@ -707,6 +707,57 @@ def im_generator(ims: list[Image.Image]) -> Generator[Image.Image, None, None]:
with Image.open(mp) as reread:
assert reread.n_frames == 3

def test_save_all_progress(self) -> None:
out = BytesIO()
progress = []

def callback(state):
if state["image_filename"]:
state["image_filename"] = (
state["image_filename"]
.replace("\\", "/")
.split("Tests/images/")[-1]
)
progress.append(state)

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

out = BytesIO()
progress = []

with Image.open("Tests/images/hopper.tif") as im:
with Image.open("Tests/images/multipage.tiff") as im2:
im.save(
out, "TIFF", save_all=True, append_images=[im2], progress=callback
)

expected = [
{
"image_index": 0,
"image_filename": "hopper.tif",
"completed_frames": 1,
"total_frames": 4,
}
]
for i in range(3):
expected.append(
{
"image_index": 1,
"image_filename": "multipage.tiff",
"completed_frames": i + 2,
"total_frames": 4,
}
)
assert progress == expected

def test_saving_icc_profile(self, tmp_path: Path) -> None:
# Tests saving TIFF with icc_profile set.
# At the time of writing this will only work for non-compressed tiffs
Expand Down
57 changes: 54 additions & 3 deletions Tests/test_file_webp.py
@@ -1,9 +1,9 @@
from __future__ import annotations

import io
import re
import sys
import warnings
from io import BytesIO
from pathlib import Path

import pytest
Expand Down Expand Up @@ -105,10 +105,10 @@ def test_write_rgb(self, tmp_path: Path) -> None:
def test_write_method(self, tmp_path: Path) -> None:
self._roundtrip(tmp_path, self.rgb_mode, 12.0, {"method": 6})

buffer_no_args = io.BytesIO()
buffer_no_args = BytesIO()
hopper().save(buffer_no_args, format="WEBP")

buffer_method = io.BytesIO()
buffer_method = BytesIO()
hopper().save(buffer_method, format="WEBP", method=6)
assert buffer_no_args.getbuffer() != buffer_method.getbuffer()

Expand All @@ -125,6 +125,57 @@ def test_save_all(self, tmp_path: Path) -> None:
reloaded.seek(1)
assert_image_similar(im2, reloaded, 1)

@skip_unless_feature("webp_anim")
def test_save_all_progress(self) -> None:
out = BytesIO()
progress = []

def callback(state):
if state["image_filename"]:
state["image_filename"] = (
state["image_filename"]
.replace("\\", "/")
.split("Tests/images/")[-1]
)
progress.append(state)

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

out = BytesIO()
progress = []

with Image.open("Tests/images/iss634.webp") as im:
im2 = Image.new("RGB", im.size)
im.save(out, "WEBP", save_all=True, append_images=[im2], progress=callback)

expected = []
for i in range(42):
expected.append(
{
"image_index": 0,
"image_filename": "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: Path) -> None:
self._roundtrip(tmp_path, self.rgb_mode, 12.5, {"icc_profile": None})
if _webp.HAVE_WEBPANIM:
Expand Down