Skip to content

Conversation

@radarhere
Copy link
Member

Pillow/src/PIL/Image.py

Lines 2513 to 2532 in b8abded

# 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()
ext = filename_ext.decode() if isinstance(filename_ext, bytes) else filename_ext
if not format:
if ext not in EXTENSION:
init()
try:
format = EXTENSION[ext]
except KeyError as e:
msg = f"unknown file extension: {ext}"
raise ValueError(msg) from e

where

Pillow/src/PIL/Image.py

Lines 636 to 640 in b8abded

def _ensure_mutable(self) -> None:
if self.readonly:
self._copy()
else:
self.load()

If the ValueError is hit, there is no need to have loaded the image first. Avoiding that is a performance improvement for this situation.

A test needed to be updated to continue testing a failed load operation, rather than an unknown file extension.

@hugovk hugovk merged commit 3c185d1 into python-pillow:main Mar 27, 2025
55 checks passed
@radarhere radarhere deleted the save_load branch March 27, 2025 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants