mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-13 02:36:17 +03:00
Fix handling of pathlib in save. Fixes #1460
This commit is contained in:
parent
73f3827e42
commit
d965257616
11
PIL/Image.py
11
PIL/Image.py
|
@ -1620,13 +1620,17 @@ class Image(object):
|
|||
"""
|
||||
|
||||
filename = ""
|
||||
open_fp = False
|
||||
if isPath(fp):
|
||||
filename = fp
|
||||
open_fp=True
|
||||
elif sys.version_info >= (3, 4):
|
||||
from pathlib import Path
|
||||
if isinstance(fp, Path):
|
||||
filename = str(fp.resolve())
|
||||
open_fp=True
|
||||
elif hasattr(fp, "name") and isPath(fp.name):
|
||||
# only set the name for metadata purposes
|
||||
filename = fp.name
|
||||
|
||||
# may mutate self!
|
||||
|
@ -1655,17 +1659,14 @@ class Image(object):
|
|||
else:
|
||||
save_handler = SAVE[format.upper()]
|
||||
|
||||
if filename:
|
||||
if open_fp:
|
||||
fp = builtins.open(filename, "wb")
|
||||
close = 1
|
||||
else:
|
||||
close = 0
|
||||
|
||||
try:
|
||||
save_handler(self, fp, filename)
|
||||
finally:
|
||||
# do what we can to clean up
|
||||
if close:
|
||||
if open_fp:
|
||||
fp.close()
|
||||
|
||||
def seek(self, frame):
|
||||
|
|
|
@ -57,6 +57,17 @@ class TestImage(PillowTestCase):
|
|||
self.assertEqual(im.mode, "RGB")
|
||||
self.assertEqual(im.size, (128, 128))
|
||||
|
||||
def test_tempfile(self):
|
||||
# see #1460, pathlib support breaks tempfile.TemporaryFile on py27
|
||||
# Will error out on save on 3.0.0
|
||||
import tempfile
|
||||
im = hopper()
|
||||
fp = tempfile.TemporaryFile()
|
||||
im.save(fp, 'JPEG')
|
||||
fp.seek(0)
|
||||
reloaded = Image.open(fp)
|
||||
self.assert_image_similar(im, reloaded, 20)
|
||||
|
||||
def test_internals(self):
|
||||
|
||||
im = Image.new("L", (100, 100))
|
||||
|
|
Loading…
Reference in New Issue
Block a user