From 4038a287eececa0d07b61005fb87292a25da80ca Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 24 Jul 2021 14:21:33 +1000 Subject: [PATCH] Corrected pathlib.Path detection when saving --- Tests/test_image.py | 9 +++++---- src/PIL/Image.py | 8 ++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index c4e6f8ade71..2d661a90327 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -149,10 +149,11 @@ def test_pathlib(self, tmp_path): assert im.mode == "RGB" assert im.size == (128, 128) - temp_file = str(tmp_path / "temp.jpg") - if os.path.exists(temp_file): - os.remove(temp_file) - im.save(Path(temp_file)) + for ext in (".jpg", ".jp2"): + temp_file = str(tmp_path / ("temp." + ext)) + if os.path.exists(temp_file): + os.remove(temp_file) + im.save(Path(temp_file)) def test_fp_name(self, tmp_path): temp_file = str(tmp_path / "temp.jpg") diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 124ca392b07..7bc4d67b576 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2180,12 +2180,12 @@ def save(self, fp, format=None, **params): filename = "" open_fp = False - if isPath(fp): - filename = fp - open_fp = True - elif isinstance(fp, Path): + if isinstance(fp, Path): filename = str(fp) open_fp = True + elif isPath(fp): + filename = fp + open_fp = True elif fp == sys.stdout: try: fp = sys.stdout.buffer