Do not load image during save if file extension is unknown

This commit is contained in:
Andrew Murray 2025-03-26 19:11:02 +11:00
parent b8abded99b
commit 88f73084f9
2 changed files with 8 additions and 8 deletions

View File

@ -43,7 +43,7 @@ def test_save() -> None:
# Arrange
with Image.open(TEST_FILE) as im:
dummy_fp = BytesIO()
dummy_filename = "dummy.filename"
dummy_filename = "dummy.h5"
# Act / Assert: stub cannot save without an implemented handler
with pytest.raises(OSError):

View File

@ -2510,13 +2510,6 @@ class Image:
# only set the name for metadata purposes
filename = os.fspath(fp.name)
# may mutate self!
self._ensure_mutable()
save_all = params.pop("save_all", False)
self.encoderinfo = {**getattr(self, "encoderinfo", {}), **params}
self.encoderconfig: tuple[Any, ...] = ()
preinit()
filename_ext = os.path.splitext(filename)[1].lower()
@ -2531,6 +2524,13 @@ class Image:
msg = f"unknown file extension: {ext}"
raise ValueError(msg) from e
# may mutate self!
self._ensure_mutable()
save_all = params.pop("save_all", False)
self.encoderinfo = {**getattr(self, "encoderinfo", {}), **params}
self.encoderconfig: tuple[Any, ...] = ()
if format.upper() not in SAVE:
init()
if save_all: