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
15 changes: 15 additions & 0 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ def test_unknown_extension(self, tmp_path: Path) -> None:
with pytest.raises(ValueError):
im.save(temp_file)

def test_failed_save_does_not_affect_next_save(self, tmp_path: Path) -> None:
im = Image.new("RGB", (1, 1))
with pytest.raises(KeyError):
im.save(io.BytesIO(), "UNKNOWN", dpi=(72, 72))
with pytest.raises(KeyError):
im.save(io.BytesIO(), "BMP", save_all=True, dpi=(72, 72))
with pytest.raises(FileNotFoundError):
im.save(tmp_path / "missing" / "temp.bmp", dpi=(72, 72))
assert not hasattr(im, "encoderinfo")

f = tmp_path / "temp.bmp"
im.save(f, dpi=(300, 300))
with Image.open(f) as reloaded:
assert reloaded.info["dpi"] == pytest.approx((300, 300), abs=0.001)

def test_internals(self) -> None:
im = Image.new("L", (100, 100))
im.readonly = 1
Expand Down
8 changes: 4 additions & 4 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2749,10 +2749,6 @@ def save(
self.load()

save_all = params.pop("save_all", None)
self._default_encoderinfo = params
encoderinfo = getattr(self, "encoderinfo", {})
self._attach_default_encoderinfo(self)
self.encoderconfig: tuple[Any, ...] = ()

if format.upper() not in SAVE:
init()
Expand All @@ -2777,6 +2773,10 @@ def save(
else:
fp = cast("IO[bytes]", fp)

self._default_encoderinfo = params
encoderinfo = getattr(self, "encoderinfo", {})
self._attach_default_encoderinfo(self)
self.encoderconfig: tuple[Any, ...] = ()
try:
save_handler(self, fp, filename)
except Exception:
Expand Down